gobo

package module
v0.0.0-...-a483eda Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2013 License: Apache-2.0 Imports: 8 Imported by: 3

README

gobo

新浪微博Go语言SDK,支持所有微博API功能

安装/更新

go get -u github.com/huichen/gobo

使用

抓取@人民日报的最近10条微博:

package main

import (
	"flag"
	"fmt"
	"github.com/huichen/gobo"
)

var (
	weibo = gobo.Weibo{}
	access_token = flag.String("access_token", "", "用户的访问令牌")
)

func main() {
	// 解析命令行参数
	flag.Parse()

	// 调用API
	var statuses gobo.Statuses
	params := gobo.Params{"screen_name": "人民日报", "count": 10}
	err := weibo.Call("statuses/user_timeline", "get", *access_token, params, &statuses)
	
	// 处理返回结果
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, status := range statuses.Statuses {
		fmt.Println(status.Text)
	}
}

用命令行参数-access_token传入访问令牌,令牌可以通过API测试工具或者gobo.Authenticator得到。

更多API调用的例子见 examples/weibo.go

Documentation

Index

Constants

View Source
const (
	ApiDomain      string = "https://api.weibo.com"
	ApiVersion     string = "2"
	UploadAPIName  string = "statuses/upload"
	ApiNamePostfix string = ".json"
)

微博API相关的常数

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessToken

type AccessToken struct {
	Access_Token string
	Remind_In    string
	Expires_In   int
	Uid          string
}

type AccessTokenInfo

type AccessTokenInfo struct {
	Uid        int64
	Appkey     string
	Scope      string
	Created_At int
	Expire_In  int
}

type Authenticator

type Authenticator struct {
	// contains filtered or unexported fields
}

Authenticator结构体实现了微博应用授权功能

func (*Authenticator) AccessToken

func (auth *Authenticator) AccessToken(code string) (AccessToken, error)

从授权码得到访问令牌

func (*Authenticator) Authorize

func (auth *Authenticator) Authorize() (string, error)

得到授权URI

func (*Authenticator) GetTokenInfo

func (auth *Authenticator) GetTokenInfo(token string) (AccessTokenInfo, error)

得到访问令牌对应的信息

func (*Authenticator) Init

func (auth *Authenticator) Init(redirectUri string, clientId string, clientSecret string) error

初始化结构体

在调用其它函数之前必须首先初始化。

func (*Authenticator) Revokeoauth2

func (auth *Authenticator) Revokeoauth2(token string) error

解除访问令牌的授权

type Comment

type Comment struct {
	Created_At    string
	Id            int64
	Text          string
	Source        string
	User          *User
	Mid           string
	Idstr         string
	Status        string
	Reply_Comment *Comment
}

type ErrorString

type ErrorString struct {
	S string
}

func (*ErrorString) Error

func (e *ErrorString) Error() string

type Geo

type Geo struct {
	Longitude     string
	Latitude      string
	City          string
	Province      string
	City_Name     string
	Province_Name string
	Address       string
	Pinyin        string
	More          string
}

type Params

type Params map[string]interface{}

Params类型用来表达微博API的JSON输入参数。注意:

  1. Params不应当包含访问令牌(access_token),因为它已经是Call和Upload函数的参数
  2. 在Upload函数中,Params参数不应当包含pic参数,上传的图片内容和类型应当通过reader和imageFormat指定

type Pic_Url

type Pic_Url struct {
	Thumbnail_Pic string
}

type Privacy

type Privacy struct {
	Comment  int
	Geo      int
	Message  int
	Realname int
	Badge    int
	Mobile   int
	Webim    int
}

type Remind

type Remind struct {
	Status         int
	Follower       int
	Cmt            int
	Dm             int
	Mention_Status int
	Mention_Cmt    int
	Group          int
	Private_Group  int
	Notice         int
	Invite         int
	Badge          int
	Photo          int
}

type Status

type Status struct {
	Created_At              string
	Id                      int64
	Mid                     string
	Text                    string
	Idstr                   string
	Source                  string
	Favorited               bool
	Trucated                bool
	In_Reply_To_Status_Id   string
	In_Reply_To_User_Id     string
	In_Reply_To_Screen_Name string
	Thumbnail_Pic           string
	Bmiddle_Pic             string
	Original_Pic            string
	Geo                     *Geo
	User                    *User
	Retweeted_Status        *Status
	Reposts_Count           int
	Comments_Count          int
	Attitudes_Count         int
	Mlevel                  int
	Visible                 *Visible
	Pic_Urls                []*Pic_Url
}

type Statuses

type Statuses struct {
	Statuses []*Status
}

type Url_Short

type Url_Short struct {
	Url_Short string
	Url_Long  string
	Type      int
	Result    bool
}

type User

type User struct {
	Id                 int64
	Idstr              string
	Screen_Name        string
	Name               string
	Province           string
	City               string
	Location           string
	Description        string
	Url                string
	Profile_Image_Url  string
	Profile_Url        string
	Domain             string
	Weihao             string
	Gender             string
	Followers_Count    int
	Friends_Count      int
	Statuses_Count     int
	Favourites_Count   int
	Created_At         string
	Following          bool
	Allow_All_Act_Msg  bool
	Geo_Enabled        bool
	Verified           bool
	Verified_Type      int
	Remark             string
	Status             *Status
	Allow_All_Comment  bool
	Avatar_Large       string
	Verified_Reason    string
	Follow_Me          bool
	Online_Status      int
	Bi_Followers_Count int
	Lang               string
}

type Visible

type Visible struct {
	Type    int
	List_Id int
}

type Weibo

type Weibo struct {
	// contains filtered or unexported fields
}

Weibo结构体定义了微博API调用功能

func (*Weibo) Call

func (weibo *Weibo) Call(method string, httpMethod string, token string, params Params, response interface{}) error

调用微博API

该函数可用来调用除了statuses/upload(见Upload函数)和微博授权(见Authenticator结构体)外的所有微博API。

输入参数

method		API方法名,比如 "/statuses/user_timeline" 又如 "comments/show"
httpMethod	HTTP请求方式,只能是"get"或者"post"之一,否则出错
token		用户授权的访问令牌
params		JSON输入参数,见Params结构体的注释
response	API服务器的JSON输出将被还原成该结构体

当出现异常时输出非nil错误

func (*Weibo) Upload

func (weibo *Weibo) Upload(token string, params Params, reader io.Reader, imageFormat string, response interface{}) error

调用/statuses/upload发带图片微博

输入参数

token		用户授权的访问令牌
params		JSON输入参数,见Params结构体的注释
reader		包含图片的二进制流
imageFormat	图片的格式,比如 "jpg" 又如 "png"
response	API服务器的JSON输出将被还原成该结构体

当出现异常时输出非nil错误

type WeiboError

type WeiboError struct {
	Err        string `json:"Error"`
	Error_Code int64
	Request    string
}

func (WeiboError) Error

func (e WeiboError) Error() string

Directories

Path Synopsis
例子程序:微博应用授权 展示功能包括得到授权URI,通过授权码得到访问令牌,获得令牌对应的信息和解除访问令牌授权等功能。
例子程序:微博应用授权 展示功能包括得到授权URI,通过授权码得到访问令牌,获得令牌对应的信息和解除访问令牌授权等功能。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL