首页 > 技术文章 > 新浪微博API

yanjiu 2013-08-23 22:20 原文

此类RESTful接口的开放平台,一般而言都采用OAuth认证方式

针对新浪微博新版接口举例如下:

1、获取code

https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI

在创建应用时需要填写YOUR_REGISTERED_REDIRECT_URI,YOUR_CLIENT_ID即是App Key

https://api.weibo.com/oauth2/authorize?client_id=2x19xxx50xx6&response_type=code&redirect_uri=https://api.weibo.com/oauth2/default.html

引导用户访问该URL,浏览器回跳转到REDIRECT_URI,同时在页面地址中包含code参数

http://www.baidu.com/?code=e89a07ef433d67251c53ab0069752056

2、获取access_token

https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
YOUR_CLIENT_ID是App key,YOUR_CLIENT_SECRET是App Secret,YOUR_REGISTERED_REDIRECT_URI是应用跳转地址,CODE是上一步中获取的code
使用Post方式提交,否则会受到405错误
http://www.baidu.com/?code=e89a07ef433d67251c53ab0069752056
https://api.weibo.com/oauth2/access_token?client_id=2178952506&client_secret=0b8519838a8f880c4b9e774e412d796f&grant_type=authorization_code&redirect_uri=http://www.baidu.com&code=2a298a0be7cec6766b60c487dca6f8ae

 得到返回数据即获得access_token

{"access_token":"2.00n97trxefa2Ff94Cdad72f155ae3Fe9D","remind_in":"103582","expires_in":103582,"uid":"1670186367"}

3、将access_token作为url的一个参数,每次请求数据时一并提交

https://api.weibo.com/2/statuses/public_timeline.json?access_token=2.00n97trxefa2Ff94Cdad72f155ae3Fe9D

得到返回数据,即是请求得到的结果

{"statuses":[{"created_at":"Fri Aug 23 22:16:46 +0800 2013","id":3614583854242053,"mid":"3614583854242053","idstr":"3614583854242053","text":"铁证如山!生女儿就是比生儿子好的证据!@全球大百科 投稿","source":"<a href=\"http://app.weibo.com/t/feed/2VDC9C\" rel=\"nofollow\">我关注的人的微博数</a>","favorited":false,"truncated":false,"in_reply_to_status_id":"","in_reply_to_user_id":"","in_reply_to_screen_name":"","pic_urls":[],"geo":null,"user":{"id":3685466602,"idstr":"3685466602","class":1,"screen_name":"GLTbU586","name":"GLTbU586","province":"100","city":"1000","location":"其他","description":"","url":"","profile_image_url":"http://tp3.sinaimg.cn/3685466602/50/0/1","profile_url":"u/3685466602","domain":"","weihao":"","gender":"m","followers_count":9,"friends_count":20,"statuses_count":9,"favourites_count":0,"created_at":"Thu Aug 01 11:26:58 +0800 2013","following":false,"allow_all_act_msg":false,"geo_enabled":true,"verified":false,"verified_type":-1,"remark":"","ptype":0,"allow_all_comment":true,"avatar_large":"http://。。。。

4、注意事项

a、在测试应用时,要将测试的账号加入到应用的测试帐户中,否则会收到403错误,显示未授权用户

b、在获取access_token时要使用Post方法,在官方指南中这一点没有明确指出

 

 

推荐阅读