新浪微博开放平台学习
Posted on Fri 27 April 2012 in 我用(IT)
关于新浪微博开放平台
From http://open.weibo.com/wiki/平台概述
微博开放平台是一个基于新浪微博客系统的开放的信息订阅、分享与交流平台。微博开放平台为您提供了海量的微博信息、粉丝关系、以及随时随地发生的信息裂变式传播渠道。 您可以登录平台并创建应用,使用微博平台提供的接口,创建有趣的应用或者让您的网站具有更强的社交特性。
用微博账号登录 经过简单的代码整合,并在自己的网站上放置微博登录按钮,您的网站用户就能够使用微博账号进行登录: 网站可以获取当前用户的用户名、头像图片、当前用户的粉丝和关注对象列表; 使用OAuth的授权机制进行开发,在网站的显著位置添加“与新浪微博连接”的功能,让用户与能够直接点击并登录。
内容发布与绑定 微博平台也提供了发布微博信息的接口,在网站上绑定微博账户后,可以在自己的网站上整合发布微博信息功能: 让用户在网站上生成的信息同步到微博网站上; 可以通过账号绑定来实时同步微博信息,让用户的关注者也能够及时了解用户的动态。
入门指南
的平台开放接口(Open API)对微博系统进行操作。 http://open.weibo.com/wiki/FAQ
-
注册成为开发者,获取AppKey 登录http://open.weibo.com ,注册成为开发者,点击创建应用,新浪将返回给你应用的AppKey
-
查看需要调用的API:http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2
-
@我的微博: http://open.weibo.com/wiki/2/statuses/mentions
-
将一个或多个短链接还原成原始的长链接 http://open.weibo.com/wiki/2/short_url/expand
-
direct_messages/new 发送一条私信 没有确切找到发私信的接口说明
-
comments/create 发送评论 http://open.weibo.com/wiki/2/comments/create
开发代码
使用新浪微博API OAuth 2 Python客户端进行调用: http://code.google.com/p/sinaweibopy 初始代码
from weibo import APIClient
Fill in your own app key/secret
consumer_key = '12345677654321'
consumer_secret = 'appleLovetwotwoand22loveapple'
callback_url = 'http://127.0.0.1/app' # callback url
accesstoken和对应的secret,现在还没有拿到
access_token = '' # app key
expires_in = '' # app expires time
获取access_token和过期时间
def getAccessCode():
print "hello!"
client = APIClient(app_key=consumer_key, app_secret=consumer_secret, redirect_uri=callback_url)
auth_url = client.get_authorize_url()
print 'Please authorize: ' + auth_url
verifier = raw_input('PIN: ').strip()
#打开那个网址后,点授权,授权以后获得pin码,回到刚才那里填入pin码,然后就能得到accesstoken和tokensecret了
print verifier
r = client.request_access_token(verifier)
access_token = r.access_token # 新浪返回的token,类似abc123xyz456
expires_in = r.expires_in # token过期的UNIX时间:http://zh.wikipedia.org/wiki/UNIX%E6%97%B6%E9%97%B4
# TODO: 在此可保存access token
print 'access_token = "%s"nexpires_in = "%s" ' % (access_token,expires_in)
client.set_access_token(access_token, expires_in)
运行getAccessCode,
hello!
Please authorize: https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A//127.0.0.1/app&response_type=code&client_id=2640860461&display=default
//把这串链接放到浏览器中,然后输入新浪微博的账号,链接返回http://127.0.0.1/app?code=
接口调用
def getMentions():
client = APIClient(app_key=consumer_key, app_secret=consumer_secret, redirect_uri=callback_url)
client.set_access_token(access_token, expires_in)
print client.get.statuses__user_timeline()
# statuses/mentions
comments = client.get.statuses__mentions().statuses
for comment in comments:
print "mentions---"+ str(comment.id) +":"+ comment.text
def addWeiboContent():
auth = OAuthHandler(consumer_key, consumer_secret)
auth.setToken(token, token_sercet)
api = API(auth)
weibo_content="hello, @li3huo!"
status = api.update_status(status=weibo_content)
print status
def addWeiboComment():
client = APIClient(app_key=consumer_key, app_secret=consumer_secret, redirect_uri=callback_url)
client.set_access_token(access_token, expires_in)
# comments/create
print client.post.comments__create(id=3423423423, comment="bingo!")
其它api https://github.com/wuyuntao/weibopy 不如sinaweibopy远矣
其他注意事项
http://open.weibo.com/wiki/应用开发者协议 Retin-A 0.05%