2013.08.30——— android 关于OAuth 2.0
参考:
http://www.cnblogs.com/lingyun1120/archive/2012/07/11/2585767.html
http://blog.csdn.net/zhongyi__gu/article/details/7081450
个人认为的oauth2.0的步骤:
1、打开webView 加载授权地址,就是用来登录用户名和密码的
它会带上一些参数
参数名 必选 介绍
client_id true 申请组件时获得的API Key
response_type true 此值固定为“token”
redirect_uri true 授权后要回调的URI,即接受code的URI。对于无Web Server的应用,其值可以是“oob”。
2、用户登录成功,就调转到你传过去的redirect url地址上去,并且将token等信息加载url参数上
3、最后,第三方sdk会处理上面的连接地址,并返回json给一个你事先写好的listenre上去
例如
1、新浪微博登录
08-30 16:59:40.660: D/Weibo-WebView(29417): onPageStarted URL: https://open.weibo.cn/oauth2/authorize?client_id=935437242&response_type=token&redirect_uri=http%3A%2F%2Fsns.whalecloud.com%2Fsina2%2Fcallback&display=mobile
08-30 17:01:28.245: D/Weibo-WebView(29417): Redirect URL: http://sns.whalecloud.com/sina2/callback#access_token=2.00ZsX5_C9xzSBB6ee1796d9bcwObtB&remind_in=295109&expires_in=295109&uid=2118866155
传给WeiboAuthListener的实现类
String mWeiboToken = values.getString("access_token");
String expires_in = values.getString("expires_in");
String mWeiboUid = values.getString("uid");
2、qq登录
08-30 16:36:42.696: D/TDialog(27989): Webview loading URL: https://openmobile.qq.com/oauth2.0/m_authorize?status_userip=10.84.237.103&scope=get_user_info%2Cget_simple_userinfo&redirect_uri=auth%3A%2F%2Ftauth.qq.com%2F&cancel_display=1&switch=1&display=mobile&status_version=15&status_os=4.0.4&sdkv=1.6&need_pay=1&pf=openmobile_android&response_type=token&status_machine=HTC+Desire+S&sdkp=a&client_id=100326440
08-30 16:41:28.325: D/TDialog(27989): Redirect URL: auth://tauth.qq.com/?#ret=0&pay_token=694012467C7773BC946CA675404D5888&pf=openmobile_android&appid=100326440&expires_in=7776000&openid=E89F8E54945A5FA46BFB7E09C3BFB5CF&pfkey=c5ec906abca224266e90579c16a7b40d&access_token=E27530D86EACBD8E6662493240AC7906
传给IUiListener的实现类
08-30 16:41:30.097: I/System.out(27989): {"ret":"0","pay_token":"694012467C7773BC946CA675404D5888","pf":"openmobile_android","appid":"100326440","openid":"E89F8E54945A5FA46BFB7E09C3BFB5CF","expires_in":"7776000","pfkey":"c5ec906abca224266e90579c16a7b40d","access_token":"E27530D86EACBD8E6662493240AC7906"}
3、facebook
09-02 14:26:03.923: D/FacebookSDK.WebDialog(27992): Webview loading URL: https://m.facebook.com/login.php?skip_api_login=1&signed_next=1&next=https%3A%2F%2Fm.facebook.com%2Fdialog%2Foauth%3Fredirect_uri%3Dfbconnect%253A%252F%252Fsuccess%26display%3Dtouch%26scope%3Duser_likes%252Cuser_status%26type%3Duser_agent%26client_id%3D673898422625724%26ret%3Dlogin&refsrc=https%3A%2F%2Fm.facebook.com%2Flogin.php&refid=9
09-02 14:26:09.308: D/FacebookSDK.WebDialog(27992): Redirect URL: https://m.facebook.com/dialog/oauth?redirect_uri=fbconnect%3A%2F%2Fsuccess&display=touch&scope=user_likes%2Cuser_status&type=user_agent&client_id=673898422625724&ret=login&ext=1378106770&hash=AebyCiOnVtjm9kop&refsrc=https%3A%2F%2Fm.facebook.com%2Flogin.php&refid=9&_rdr#_=_
09-02 14:26:51.019: I/PRIME(27992): <CallBackProxy> Send to WebViewClient.
09-02 14:26:51.019: D/FacebookSDK.WebDialog(27992): Redirect URL: fbconnect://success#access_token=CAAJk6DQYZAbwBAB2Diz8ZCobc2vAEQMDQfmVWRVZB6BBI8dEM8NUOV8TblMwg8hsVp4refsy0aDPniOIxzZCuvxZCIIZCVheIBFxJYZBEcSVhuFiBw8G5eO9XadqOZBxMl3pZBp8ZCgcDQEWSxEZBHG2vCQdAuMZA4lN4AIntnoSelG6p6ZBTOBT0wfqH&expires_in=5184000
mBtnFacebook.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
mFacebookUser = user;
if(mFacebookUser!=null){
UtilLog.log(TAG, user.getId() + " userId");
Toast.makeText(_context, R.string.livinglogin_str_suc, Toast.LENGTH_SHORT).show();
if(_dialog==null){
_dialog = new ProgressDialog(_context);
}
_dialog.setMessage(getString(R.string.dialog_loginroom_loginhall));
_dialog.show();
mHandler.sendEmptyMessageDelayed(MSG_TIMEOUT, 15000);
Session session = Session.getActiveSession();
String[] obj = {mFacebookUser.getId(), session.getAccessToken()};
TaskManager.userLoginFacebookTask(mHandler, obj);
}else{
UtilLog.log(TAG, "null userId");
}
}
});
4、twitter
09-02 14:35:12.508: I/LivingLoginTwitterWebActivity(27992): onPageStartedhttp://api.twitter.com/oauth/authenticate?oauth_token=ywT3I1KHLMC6s8EU6BeqBqoRO7PLWcT4CoHilW
09-02 14:36:47.501: I/LivingLoginTwitterWebActivity(27992): onPageStartedhttps://api.twitter.com/oauth/authorize
09-02 15:01:26.584: I/LivingLoginTwitterWebActivity(27992): shouldOverrideUrlLoading http://show.sina.com.cn/?oauth_token=bzmBpKo6szq9q7011kuaUSPhKjXkvzYslzfPWG3U&oauth_verifier=eec6NiLvDV8nAfqAMp9ovbqmFYb8LBzvmCb4vHT3M
String tOauthVerifier = data.getStringExtra("oauth_verifier");
try {
AccessToken at = mTwitter.getOAuthAccessToken(mTwitterRequestToken, tOauthVerifier);
String accessToken = at.getToken();
String accessTokenSecret = at.getTokenSecret();
long userId = at.getUserId();
mTwitter.setOAuthAccessToken(at); //这里要设置access_token
// mPrefs.setTwitterAccessToken(accessToken); //记得存入文件系统
// mPrefs.setTwitterAccessTokenSecret(accessTokenSecret);
Toast.makeText(_context, R.string.livinglogin_str_suc, Toast.LENGTH_SHORT).show();
System.out.println("userId: " + userId + ", accessToken: " + accessToken + ", accessTokenSecret: " + accessTokenSecret
+ ", toauthVerifier: " + tOauthVerifier);
if(_dialog==null){
_dialog = new ProgressDialog(_context);
}
_dialog.setMessage(getString(R.string.dialog_loginroom_loginhall));
_dialog.show();
mHandler.sendEmptyMessageDelayed(MSG_TIMEOUT, 15000);
String[] obj = {userId + "", accessToken, accessTokenSecret, tOauthVerifier};
TaskManager.userLoginTwitterTask(mHandler, obj);
} catch (TwitterException e) {
e.printStackTrace();
}