2012年12月14日星期五

新浪微博开放平台应用之登录授权

在前文《新浪微博开放平台应用之数据抓取》中,我们学会了如何使用 trends/statuses 接口抓取话题数据,相比 trends/statuses 接口,有些抓取数据的接口需要登录授权后才能调用,比如获取评论的接口 2/comments/show。下面我们就来看如何进行登录授权。

完成登录需要用到 oauth2/authorize 接口,其接收以下参数:
  • client_id: 所申请的app_key
  • response_type: 返回数据类型,值可为code或state,code用于后续获取access_token
  • display: 授权页面的终端类型,default指示游览器
  • redirect_uri: 授权回调地址,需与开放平台中设置的回调地址一致

参数既可以以POST方式传送,也可以以GET方式发送,如下例子:
https://api.weibo.com/oauth2/authorize?redirect_uri=http://liuxiaofang.sinaapp.com/callback?url=/init-comments&ids=3522096338448283&response_type=code&client_id=622387540&display=default

以上url以人为可读的方式显示,向应用服务器发送前还得经过编码(如使用python中的urllib.quote)。

这里所说的回调地址,通过 应用页面 -> 接口管理 -> 授权设置 进行配置。














正确发送URL后,将进入以下登录界面:













成功登录后,将跳转到我们之前设定的 redirect_uri,并返回 code 值:
http://liuxiaofang.sinaapp.com/callback?url=/init-comments&ids=3522096338448283&code=123456

有了code,我们就可以请求获取access_token,获取 access_token 的接口为 oauth2/access_token,其接收以下参数:
  • grant_type: 请求类型,对应与调用 authorize 获得的code,这里值应为 authorization_code
  • code: 以上获得的 code 值
  • client_id: 所申请的 app_key
  • client_secret: 所申请的 app_secret
  • redirect_uri: 回调地址,需与开放平台中设置的回调地址一致
向 oauth2/access_token 传送参数,需用POST方式,如:
https://api.weibo.com/oauth2/access_token
grant_typeauthorization_code
client_id622387540
client_secret = 123456
redirect_urihttp://liuxiaofang.sinaapp.com/callback?
code = 123456

正确发送URL后,将跳转到类似以下授权页面:















完成授权后,将跳转到之前设定的回调地址,并且从 response 中,我们可以获取到 access_token 和 expires_in 超时值。

有了 access_token,我们终于可以使用 2/comments/show 这类需要事先登录授权的接口了。下面通过GET方式获取指定微博id的评论,获取到的 access_token 放置在请求头中:

https://api.weibo.com/2/comments/show?id=3522885349661782
Authorization: OAuth2 123456

之后新浪服务器将返回评论id、评论文本、评论创建时间、评论作者等信息。

欢迎访问我的一个基于sae和新浪开放平台的网站 带上猫咪去旅行

Have fun!

1 条评论:

  1. And his Houston Rockets for the first time, jeremy lin jersey of the season did not start as

    the outside look forward to as the world's best. However, he was in his own way, toward the road played an excellent season ahead. He

    knows its own is not enough on the all-star level, his thoughts right. Jeremy Lin just continue to persevere, to reduce errors and improve

    the hit rate (currently 42%), which will contribute to that he played a wonderful second half.

    Asik major james harden jersey significance for the Rockets, his performance in the fifth

    position, makes it gradually forgotten prevailed for a whole summer of Dwight - Howard and Andrew - Andrew Bynum is better. As we all

    know, on the offensive end, he also compared jerky, so he is now averaging 10.3 points, is definitely a surprise gift. To say he needs to

    rise up and looks like some nitpicking, but true.

    Look at it the first time if he can forward pass attack ability, then, how much he will be lethal on the offensive end. If he can improve the

    hands the ball sense, the rocket will be more easy scoring opportunities. When he had the ball, he just tough some think more dunk,

    rather than merely layup. This takes time, despite not outstanding athletic ability, but he was an excellent decision capabilities and game

    attitudejeremy lin houston jerseybe remedied.

    回复删除