Net::SinaWeibo - A simple and lightweight OAuth api for SinaWeibo
version 0.003
# from sinaweibo app setting my $app_key = 'xxxx'; my $app_key_secret = 'xxxxxxxxx'; my $client = Net::SinaWeibo->new( app_key => $app_key, app_key_secret => $app_key_secret); # authorization my $callback_url = 'http://youdomain.com/app_callback'; my $url = $client->get_authorize_url(callback_url => $callback_url); # or don't use callback_url,just like a desktop client. my $url = $client->get_authorize_url; # now $client hold the request_token, but you must authorize your app first. # let user go to visit the authorize url. say 'Please goto this url:',$url; # save these tokens to your file. Net::SinaWeibo->save_tokens('~/app/var/tokens/my.tokens', app_key => $app_key, app_key_secret => $app_key_secret, _request_token => $client->request_token->token, _request_token_secret => $client->request_token->secret, ); # later,you can load tokens my %tokens = Net::SinaWeibo->load_tokens '~/app/var/tokens/my.tokens'; # After user authorized,you can request access_token with the request token my $client = Net::SinaWeibo->new( app_key => $tokens{app_key}, app_secret => $tokens{app_secret}, tokens => { request_token => $tokens{_request_token}, request_token_secret => $tokens{_request_token_secret}, } ); my $verifier = '5123876'; my ($access_token,$access_token_secret) = $client->get_access_token( verifier => $verifier, ); # now you can retrieve any restricted resources. my $friends = $client->friends; # any api can pass any specific parameters my $latest_mentions = $client->mentions since_id => 25892384,count => 10,page => 1; # upload also support. my $ok = $client->upload(status => 'Hello,this first image file', pic => 'images/demo.jpg'); # profile image my $ok = update_profile_image(image => 'images/my_avatar.jpg'); # enjoy!
This is a lite OAuth client for SinaWeibo(http://t.sina.com.cn/).
my $client = Net::SinaWeibo->new( app_key => 'sinaweibo_app_key', app_secret => 'sina_weibo_app_secret', # optional,you can pass access_token/request_token tokens => { access_token => 'xxxxxx', access_token_secret => 'xxxxxxxx', # or request_token => 'xxxxxxx', request_token_secret => 'xxxxx', } );
Url which service provider redirect end-user to after authorization.
Get the URL to authorize a user as a URI object.
Request the request token and request token secret for this user.
This is called automatically by get_authorize_url if necessary.
get_authorize_url
Verfication code which SinaWeibo returns.
Request token object. Optional, if you has been set request_token.
my $access_token = $sina->get_access_token(verifier => '589893'); # or my $access_token = $sina->get_access_token(verifier => '589893',token => $request_token);
Request the access token for this user.
The user must have authorized this app at the url given by get_authorize_url first.
Returns the access token but also sets them internally so that after calling this method you can immediately call a restricted method.
Get the last called api(uri)
Get the last api error hash ref. If the error message is not any valid error response, will just return the raw response content.
my %tokens = Net::SinaWeibo->load_tokens('saved.tokens');
A convenience method for loading tokens from a config file.
Returns a hash with the token names suitable for passing to new().
new()
Returns an empty hash if the file doesn't exist.
Get last api error_code, which return by provider. If provider reponse is not valid JSON message, it's just the http status code.
Get detail error code about the api error (like 400 serial).
Net::SinaWeibo->save_tokens( consumer_token => 'xxxx', consumer_secret => 'xxxx', _request_token => 'xxxxxx', _request_token_secret => 'xxxxx', _access_token => 'xxxxx', _access_secret => 'xxxxx, )
A convenience method to save a hash of tokens out to the given file.
Follow are generated proxy method for SinaWeibo API.
Recent document please visit http://open.t.sina.com.cn/wiki/
返回最新更新的20条微博消息。
count: 每次返回的最大记录数,不能超过200,默认20.
http://open.t.sina.com.cn/wiki/index.php/Statuses/public_timeline
返回用户所有关注用户最新n条微博信息。和用户“我的首页”返回内容相同。
since_id: 微博信息ID. 只返回ID比since_id大(比since_id时间晚的)的微博信息内容。 max_id: 微博信息ID. 返回ID不大于max_id的微博信息内容。 count: 每次返回的最大记录数,不能超过200,默认20. page: 返回结果的页序号。注意:有分页限制。根据用户关注对象发表的数量,通常最多返回1,000条最新微博分页内容, 默认1
http://open.t.sina.com.cn/wiki/index.php/Statuses/friends_timeline
获取用户发布的微博信息列表
id: 可选参数. 根据指定用户UID或微博昵称来返回微博信息。 user_id: 可选参数. 用户UID,主要是用来区分用户UID跟微博昵称一样,产生歧义的时候,特别是在微博昵称为数字导致和用户Uid发生歧义。 screen_name:可选参数.微博昵称,主要是用来区分用户UID跟微博昵称一样,产生歧义的时候。 since_id:可选参数(微博信息ID). 只返回ID比since_id大(比since_id时间晚的)的微博信息内容 max_id: 可选参数(微博信息ID). 返回ID不大于max_id的微博信息内容。 count: 可选参数. 每次返回的最大记录数,最多返回200条,默认20。 page: 可选参数. 分页返回。注意:最多返回200条分页内容。
http://open.t.sina.com.cn/wiki/index.php/Statuses/user_timeline
获取@当前用户的微博列表
since_id. 可选参数. 返回ID比数值since_id大(比since_id时间晚的)的提到。 max_id. 可选参数. 返回ID不大于max_id(时间不晚于max_id)的提到。 count. 可选参数. 每次返回的最大记录数(即页面大小),不大于200,默认为20。 page. 可选参数. 返回结果的页序号。注意:有分页限制。
http://open.t.sina.com.cn/wiki/index.php/Statuses/mentions
获取当前用户发送及收到的评论列表
since_id: 可选参数(评论ID). 只返回ID比since_id大(比since_id时间晚的)的评论。 max_id: 可选参数(评论ID). 返回ID不大于max_id的评论。 count: 可选参数. 每次返回的最大记录数,不大于200,默认20。 page: 可选参数. 返回结果的页序号。注意:有分页限制。
http://open.t.sina.com.cn/wiki/index.php/Statuses/comments_timeline
获取当前用户发出的评论
http://open.t.sina.com.cn/wiki/index.php/Statuses/comments_by_me
获取当前用户收到的评论
http://open.t.sina.com.cn/wiki/index.php/Statuses/comments_to_me
获取指定微博的评论列表
id. 必选参数. 返回指定的微博ID count. 可选参数. 每次返回的最大记录数(即页面大小),不大于200,默认为20。 page. 可选参数. 返回结果的页序号
http://open.t.sina.com.cn/wiki/index.php/Statuses/comments
批量获取一组微博的评论数及转发数,一次请求最多获取100个。
ids. 必填参数. 微博ID号列表,用逗号隔开
http://open.t.sina.com.cn/wiki/index.php/Statuses/counts
获取当前用户未读消息数
with_new_status 可选参数,默认为0。1表示结果包含是否有新微博,0表示结果不包含是否有新微博。 since_id 可选参数 参数值为微博id,返回此条id之后,是否有新微博产生,有返回1,没有返回0
http://open.t.sina.com.cn/wiki/index.php/Statuses/unread
未读消息数清零接口
type 需要清零的计数类别,值为下列四个之一:1--评论数,2--@数,3--私信数,4--关注我的数。
http://open.t.sina.com.cn/wiki/index.php/Statuses/reset_count
表情接口,获取表情列表
type:表情类别,可选参数,"face":普通表情,"ani":魔法表情,"cartoon":动漫表情;默认为"face" language:语言类别,可选参数,"cnname"简体,"twname"繁体;默认为"cnname"
http://open.t.sina.com.cn/wiki/index.php/Emotions
根据ID获取单条微博信息内容
id. 必须参数(微博信息ID),要获取已发表的微博ID,如ID不存在返回空
http://open.t.sina.com.cn/wiki/index.php/Statuses/show
返回根据微博ID和用户ID生成的单条微博页面url
发布一条微博信息
status. 必填参数, 要更新的微博信息。必须做URLEncode,信息内容不超过140个汉字,为空返回400错误。 in_reply_to_status_id. 可选参数,@ 需要回复的微博信息ID, 这个参数只有在微博内容以 @username 开头才有意义。(即将推出)。 lat. 可选参数,纬度,发表当前微博所在的地理位置,有效范围 -90.0到+90.0, +表示北纬。只有用户设置中geo_enabled=true时候地理位置才有效。(仅对受邀请的合作开发者开放) long. 可选参数,经度。有效范围-180.0到+180.0, +表示东经。(仅对受邀请的合作开发者开放)
如果没有登录或超过发布上限,将返回403错误. 如果in_reply_to_status_id不存在,将返回500错误. 系统将忽略重复发布的信息。每次发布将比较最后一条发布消息,如果一样将被忽略。因此用户不能连续提交相同信息。
http://open.t.sina.com.cn/wiki/index.php/Statuses/update
上传图片并发布一条微博信息
status. 必填参数, 要更新的微博信息。必须做URLEncode,信息内容不超过140个汉字。支持全角、半角字符。 pic. 必填参数。仅支持JPEG,GIF,PNG图片,为空返回400错误。目前上传图片大小限制为<1M。 lat. 可选参数,纬度,发表当前微博所在的地理位置,有效范围 -90.0到+90.0, +表示北纬。只有用户设置中geo_enabled=true时候地理位置才有效。(保留字段,暂不支持) long. 可选参数,经度。有效范围-180.0到+180.0, +表示东经。(保留字段,暂不支持)
如果使用的Oauth认证,图片参数pic不参与签名。
http://open.t.sina.com.cn/wiki/index.php/Statuses/upload
删除一条微博信息
id. 必须参数. 要删除的微博ID.
http://open.t.sina.com.cn/wiki/index.php/Statuses/destroy
转发一条微博信息(可加评论)
id 必填参数, 转发的微博ID status. 可选参数, 添加的转发信息。必须做URLEncode,信息内容不超过140个汉字。如不填则自动生成类似“转发 @author: 原内容”文字。
如果没有登录,将返回403错误. 转发的微博不存在,将返回500错误. http://open.t.sina.com.cn/wiki/index.php/Statuses/repost.
对一条微博信息进行评论
id 必填参数, 要评论的微博id comment. 必填参数, 评论内容。必须做URLEncode,信息内容不超过140个汉字。
http://open.t.sina.com.cn/wiki/index.php/Statuses/comment
删除当前用户的微博评论信息
id. 必须参数. 要删除的评论ID.
如果评论不存在,将返回403错误.
http://open.t.sina.com.cn/wiki/index.php/Statuses/comment_destroy
批量删除当前用户的微博评论信息
ids 必选参数,想要删除评论的id,多个id之间用半角逗号分割,支持最多20个。
http://open.t.sina.com.cn/wiki/index.php/Statuses/comment/destroy_batch
回复微博评论信息
id 必填参数, 要评论的微博id cid 必填参数, 要评论的评论id 如没有或非法则为对微博的评论 comment. 必填参数, 评论内容。必须做URLEncode,信息内容不超过140个汉字
http://open.t.sina.com.cn/wiki/index.php/Statuses/reply
获取系统推荐用户
category: 分类,可选参数,返回某一类别的推荐用户,默认为 default。如果不在一下分类中,返回空列表: default:人气关注 ent:影视名星 hk_famous:港台名人 model:模特 cooking:美食&健康 sport:体育名人 finance:商界名人 tech:IT互联网 singer:歌手 writer:作家 moderator:主持人 medium:媒体总编 stockplayer:炒股高手
http://open.t.sina.com.cn/wiki/index.php/Users/hot
根据用户ID获取用户资料(授权用户)
id. 用户UID或微博昵称。 user_id. 指定用户UID,主要是用来区分用户UID跟微博昵称一样,产生歧义的时候,特别是在用户账号为数字导致和用户Uid发生歧义 screen_name. 指定微博昵称,主要是用来区分用户UID跟微博昵称一样,产生歧义的时候。
ID或者昵称不存在返回400错误.
http://open.t.sina.com.cn/wiki/index.php/Users/show
获取当前用户关注对象列表及最新一条微博信息
id. 用户UID或微博昵称。 user_id. 指定用户UID,主要是用来区分用户UID跟微博昵称一样,产生歧义的时候,特别是在用户账号为数字导致和用户Uid发生歧义 screen_name. 指定微博昵称,主要是用来区分用户UID跟微博昵称一样,产生歧义的时候。 cursor. 选填参数. 单页只能包含100个关注列表,为了获取更多则cursor默认从-1开始,通过增加或减少cursor来获取更多, 如果没有下一页,则next_cursor返回0 count. 可选参数. 每次返回的最大记录数(即页面大小),不大于200,默认返回20。
如果没有提供cursor参数,将只返回最前面的100个关注列表。当以Json方式返回时,返回结构会稍有不同。
http://open.t.sina.com.cn/wiki/index.php/Statuses/friends
获取当前用户粉丝列表及最新一条微博信息
如果没有提供cursor参数,将只返回最前面的100个列表.
http://open.t.sina.com.cn/wiki/index.php/Statuses/followers
获取当前用户最新私信列表
since_id. 可选参数. 返回ID比数值since_id大(比since_id时间晚的)的私信。 max_id. 可选参数. 返回ID不大于max_id(时间不晚于max_id)的私信。 count. 可选参数. 每次返回的最大记录数(即页面大小),不大于200。 page. 可选参数. 返回结果的页序号。注意:有分页限制。
http://open.t.sina.com.cn/wiki/index.php/Direct_messages
获取当前用户发送的最新私信列表
http://open.t.sina.com.cn/wiki/index.php/Direct_messages/sent
发送一条私信
id: 必须参数. UID或微博昵称. 为了支持数字的微博昵称,需选填写下面2个参数screen_name或user_id: screen_name: 微博昵称 user_id: 新浪UID text: 必须参数. 要发生的消息内容,需要做URLEncode,文本大小必须小于300个汉字.
http://open.t.sina.com.cn/wiki/index.php/Direct_messages/new
删除一条私信
id. 必填参数,要删除的私信主键ID.
http://open.t.sina.com.cn/wiki/index.php/Direct_messages/destroy
批量删除私信
ids 必选参数,想要删除私信的id,多个id之间用半角逗号分割,支持最多20个。
http://open.t.sina.com.cn/wiki/index.php/Direct_messages/destroy_batch
关注某用户
id: 要关注的用户UID或微博昵称 user_id: 要关注的用户UID,主要是用在区分用户UID跟微博昵称一样,产生歧义的时候。 screen_name: 要关注的微博昵称,主要是用在区分用户UID跟微博昵称一样,产生歧义的时候。
目前的最多关注2000人,失败则返回一条字符串的说明。如果已经关注了此人,则返回http 403的状态。关注不存在的ID将返回400。
http://open.t.sina.com.cn/wiki/index.php/Friendships/create
取消关注.成功则返回被取消关注人的资料,失败则返回一条字符串的说明。
id. 必填参数. 要取消关注的用户UID或微博昵称 user_id. 必填参数. 要取消关注的用户UID,主要是用在区分用户UID跟微博昵称一样,产生歧义的时候。 screen_name. 必填参数. 要取消的微博昵称,主要是用在区分用户UID跟微博昵称一样,产生歧义的时候。
http://open.t.sina.com.cn/wiki/index.php/Friendships/destroy
获取两个用户关系的详细情况
以下参数可不填写,如不填,则取当前用户
source_id. 源用户UID source_screen_name. 源微博昵称
下面参数必须选填一个:
target_id. 要判断的目的用户UID target_screen_name. 要判断的目的微博昵称
如果源用户或目的用户不存在,将返回http的400错误. 返回的blocking表示source_id用户是否对target_id加黑名单,只对source_id是当前用户有效,即只能看到自己的阻止设置(blocking协议暂不支持返回)
http://open.t.sina.com.cn/wiki/index.php/Friendships/show
获取用户关注对象uid列表
如果没有提供cursor参数,将只返回最前面的5000个关注id
http://open.t.sina.com.cn/wiki/index.php/Friends/ids
获取用户粉丝对象uid列表
如果没有提供cursor参数,将只返回最前面的5000个粉丝id
http://open.t.sina.com.cn/wiki/index.php/Followers/ids
设置隐私信息
comment: 谁可以评论此账号的微薄。 0:所有人 1:我关注的人 默认为0 message:谁可以给此账号发私信。0:所有人 1:我关注的人 默认为1 realname 是否允许别人通过真实姓名搜索到我,值---0允许,1不允许,默认值1 geo 发布微博,是否允许微博保存并显示所处的地理位置信息。值—0允许,1不允许,默认值0 badge 勋章展现状态,值—1私密状态,0公开状态,默认值0
http://open.t.sina.com.cn/wiki/index.php/Account/update_privacy
获取隐私信息
http://open.t.sina.com.cn/wiki/index.php/Account/get_privacy
将某用户加入黑名单
必选参数(至少选一个):
user_id:要加入黑名单的用户ID。 screen_name:要加入黑名单的用户微博昵称,可选。
user_id或screen_name若不存在返回400
http://open.t.sina.com.cn/wiki/index.php/Blocks/create
将某用户移出黑名单
user_id:要删除黑名单的用户ID screen_name:要删除黑名单的用户昵称
http://open.t.sina.com.cn/wiki/index.php/Blocks/destroy
某用户是否是黑名单用户
user_id:要检查的用户ID screen_name:要检查的用户昵称
http://open.t.sina.com.cn/wiki/index.php/Blocks/exists
列出黑名单用户(输出用户详细信息)
page. 页码,可选。. count. 一页大小,可选。.
http://open.t.sina.com.cn/wiki/index.php/Blocks/blocking
列出分页黑名单用户(只输出id)
http://open.t.sina.com.cn/wiki/index.php/Blocks/blocking/ids
返回指定用户的标签列表
user_id: 必填参数,查询用户的ID count: 可选参数. 每次返回的最大记录数(即页面大小),不大于200,默认为20。 page: 可选参数. 返回结果的页序号。注意:有分页限制。
http://open.t.sina.com.cn/wiki/index.php/Tags
添加用户标签
tags: 标签,必填参数,多个标签之间用逗号间隔
http://open.t.sina.com.cn/wiki/index.php/Tags/create
返回用户感兴趣的标签
page: 可选参数,页码,默认为1 count: 可选参数,分页大小,默认为10
http://open.t.sina.com.cn/wiki/index.php/Tags/suggestions
删除标签
tag_id:标签ID,必填参数
http://open.t.sina.com.cn/wiki/index.php/Tags/destroy
批量删除标签
ids:必选参数,要删除的tag id,多个id用半角逗号分割,最多20个。
http://open.t.sina.com.cn/wiki/index.php/Tags/destroy_batch
验证当前用户身份是否合法.
如果用户新浪通行证身份验证成功且用户已经开通微博则返回 http状态为 200;如果是不则返回401的状态和错误信息。此方法用了判断用户身份是否合法且已经开通微博。
http://open.t.sina.com.cn/wiki/index.php/Account/verify_credentials
获取当前用户API访问频率限制
http://open.t.sina.com.cn/wiki/index.php/Account/rate_limit_status
当前用户退出登录.清除已验证用户的session,退出登录,并将cookie设为null。主要用于widget等web应用场合。
http://open.t.sina.com.cn/wiki/index.php/Account/end_session
更改资料.
必须有一下参数中的一个或多个,参数值为字符串. 进一步的限制,请参阅下面的各个参数描述.
name. 昵称,可选参数.不超过20个汉字 gender 性别,可选参数. m,男,f,女。 province 可选参数. 参考省份城市编码表 city 可选参数. 参考省份城市编码表,1000为不限 description. 可选参数. 不超过160个汉字.
http://open.t.sina.com.cn/wiki/index.php/Account/update_profile
更新用户头像
image.必须参数. 必须为小于700K的有效的GIF, JPG, 或 PNG 图片. 如果图片大于500像素将按比例缩放。
http://open.t.sina.com.cn/wiki/index.php/Account/update_profile_image
获取当前用户的收藏列表
page: 可选参数. 返回结果的页序号。注意:有分页限制。
http://open.t.sina.com.cn/wiki/index.php/Favorites
添加收藏.
id 必填参数, 要收藏的微博id
http://open.t.sina.com.cn/wiki/index.php/Favorites/create
删除当前用户收藏的微博信息
id. 必须参数. 要删除的收藏微博信息ID.
http://open.t.sina.com.cn/wiki/index.php/Favorites/destroy
批量删除收藏的微博信息
ids 必选参数,想要删除收藏微博的id,多个id之间用半角逗号分割,支持最多20个。
http://open.t.sina.com.cn/wiki/index.php/Favorites/destroy_batch
The latest code for this module can be found at
http://github.com/nightsailer/net-sinaweibo.git
Author blog: (Chinese)
http://nightsailer.com/
You can find documentation for this module with the perldoc command.
perldoc Net::SinaWeibo
You can also look for information at:
http://github.com/nightsailer/net-sinaweibo/issues
http://open.t.sina.com.cn/wiki/
Pan Fan(nightsailer) <nightsailer@gmail.com>
This software is copyright (c) 2010 by Pan Fan(nightsailer).
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Net::SinaWeibo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::SinaWeibo
CPAN shell
perl -MCPAN -e shell install Net::SinaWeibo
For more information on module installation, please visit the detailed CPAN module installation guide.