首页 > 代码库 > 第三方登录(一)
第三方登录(一)
近期两天都在研究第三方登录。为了温习下流程,和避免以后又走上弯路,留下一丝痕迹,也方便大家參考。如有错误请大家指正。
一開始我是先弄微信的第三方登录,结果发现貌似如今是要付费才干够?
然后就屁颠屁颠的去弄新浪微博的了:http://open.weibo.com/connect
依照他的流程走完你应该就能获得app key 和 app secret了。
这里先说下没审核上线前仅仅能自己加入15个账号用以授权:(微博名)
然后去设置回调页:
新浪微博第三方这里遇到问题不多,非常快就好了。以下讲具体实现。
先看看接口文档
接口文档
接口 | 说明 |
---|---|
OAuth2/authorize | 请求用户授权Token |
OAuth2/access_token | 获取授权过的Access Token |
OAuth2/get_token_info | 授权信息查询接口 |
OAuth2/revokeoauth2 | 授权回收接口 |
OAuth2/get_oauth2_token | OAuth1.0的Access Token更换至OAuth2.0的Access Token |
1. 引导须要授权的用户到例如以下地址:
1 |
|
YOUR_CLIENT_ID是你的app key,如今要获取code所以 client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI" style="font-family:‘Courier New‘,Consolas,‘Bitstream Vera Sans Mono‘,Courier,monospace; white-space:nowrap; text-decoration:none; line-height:2em!important; color:rgb(0,0,0)!important; border:0px!important; bottom:auto!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important">response_type=code即可了。 client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI" style="font-family:‘Courier New‘,Consolas,‘Bitstream Vera Sans Mono‘,Courier,monospace; white-space:nowrap; text-decoration:none; line-height:2em!important; color:rgb(0,0,0)!important; border:0px!important; bottom:auto!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important">YOUR_REGISTERED_REDIRECT_URI就是你要回调的页面,注意域名匹配在新浪上填写的,这里的实现能够是直接一个链接,或者跳到回调页面进行推断处理再重定向到这个url
2. 假设用户允许授权。页面跳转至 YOUR_REGISTERED_REDIRECT_URI/?
code=CODE
这里就能够得到code。这里回调到页面就能够处理接下来的一步了
3. 换取Access Token
1 | https://api.weibo.com/oauth2/access_token? |
YOUR_CLIENT_SECRET是你的app secret, client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE" style="font-family:‘Courier New‘,Consolas,‘Bitstream Vera Sans Mono‘,Courier,monospace; white-space:nowrap; text-decoration:none; line-height:2em!important; color:rgb(0,0,0)!important; border:0px!important; bottom:auto!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:0px 0px!important">grant_type=authorization_code就这样写。然后code是你刚刚获得的code值,到了回调页面你就能够到后台处理了,这里我一開始用ajax去请求结果发现跨域请求jquery不能接受数据的返回。并且这里返回会带有access_token。官方有说这个的获取建议放在服务端,避免被劫持。所以后来我便放到后台处理。首先获取code值,然后用post的方式请求。记得新浪微博这里用post方式,不然一直报错。
我用的是php。详细的后台发送请求请看:http://blog.csdn.net/nohaoye/article/details/46894205
当中client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET能够使用basic方式增加header中,返回值
请求成功了就会返回数据,可是不是官方的以下这个,返回还会有一个用户唯一标识uid。新浪微博是一步就都获取了。
1 2 3 4 5 | { "access_token" : "SlAV32hkKG" , "remind_in" : 3600, "expires_in" : 3600 } |
4. 使用获得的Access Token调用API
新浪微博第三方登录就写到这。之后是QQ第三方登录。
第三方登录(一)