码迷,mamicode.com
首页 > 移动开发 > 详细

Android第三方授权(新浪微博篇)

时间:2015-04-18 20:21:42      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

Android第三方认证新浪微博,相对微信,也比较简单,并且sina给了一个sdk和sdkdemo,这个demo封装了许多,但是自己不准备记录这个demo,而是直接使用sdk

同样去sina官方下载sdk,自己去搜吧,地址不给了

1.初始化SsoHandler

1 AuthInfo mWeiboAuthInfo = new AuthInfo(this, Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE);
2 SsoHandler mSsoHandler = new SsoHandler(this, mWeiboAuthInfo);

其中几个常量的解释可以到官网查看

2.开始授权

 1 mSsoHandler.authorize(new MyWeiboAuthListener(new LoginWeiboGetUserInfoListener()));    
 2 private static class MyWeiboAuthListener implements WeiboAuthListener {
 3 
 4         private RequestListener mRequestUserInfoListener = null;
 5         
 6         public MyWeiboAuthListener(RequestListener requestUserInfoListener){
 7             this.mRequestUserInfoListener = requestUserInfoListener;
 8         }
 9         
10         @Override
11         public void onCancel() {
12             // TODO Auto-generated method stub
13             Toast.makeText(mContext, "已取消微博登录", Toast.LENGTH_SHORT).show();
14         }
15 
16         @Override
17         public void onComplete(Bundle values) {
18             // TODO Auto-generated method stub
19             mAccessToken = Oauth2AccessToken.parseAccessToken(values);
20             if (mAccessToken.isSessionValid()) {
21                 // 保存 Token 到 SharedPreferences
22                 WeiboParameters weiboParameters = new WeiboParameters(
23                         Constants.APP_KEY);
24                 weiboParameters.put("access_token", mAccessToken.getToken());
25 
26                 if( mWeiboRunner == null ){
27                     mWeiboRunner = new AsyncWeiboRunner(
28                         mContext);
29                 }
30                 
31                 if( mRequestUserInfoListener == null ){
32                     return;
33                 }
34                 mWeiboRunner.requestAsync(WEIBO_USERID_URL,
35                         weiboParameters, "POST",
36                         mRequestUserInfoListener);
37 
38             } else {
39                 // 当您注册的应用程序签名不正确时,就会收到 Code,请确保签名正确
40                 String code = values.getString("code", "");
41                 Log.i(TAG, "应用签名不正确 code = " + code);
42                 Toast.makeText(mContext, "微博认证失败", Toast.LENGTH_SHORT).show();
43 
44             }
45         }
46 
47         @Override
48         public void onWeiboException(WeiboException arg0) {
49             // TODO Auto-generated method stub
50 
51         }
52 
53     }
54 
55 
56 /**
57      * 获取用户信息
58      * @author ReCwert
59      *
60      */
61     private static class LoginWeiboGetUserInfoListener implements RequestListener {
62     
63         @Override
64         public void onComplete(String result) {
65             // TODO Auto-generated method stub
66             JSONObject userObject = JSON.parseObject(result);
67             String weiboUid = userObject.getString("uid");
68             if( TextUtils.isEmpty(weiboUid)){
69                 Log.e(TAG, "weibo uid = null ");
70                 return;
71             }
72             
73             Log.i(TAG, "weibo uid = " + weiboUid );
74             uid = weiboUid;
75             WeiboParameters weiboParameters = new WeiboParameters(Constants.APP_KEY);
76             weiboParameters.put("uid", weiboUid);
77             weiboParameters.put("access_token", mAccessToken.getToken());
78             if( mWeiboRunner == null ){
79                 mWeiboRunner = new AsyncWeiboRunner(
80                     mContext);
81             }
82             
83             mWeiboRunner.requestAsync(WEIBO_USERINFO_URL, weiboParameters, "GET", new MyWeiboHandleUserInfoListener());
84         }
85 
86         @Override
87         public void onWeiboException(WeiboException exception) {
88             // TODO Auto-generated method stub
89             exception.printStackTrace();
90             
91         }
92 
93     }

 1 /**
 2      * 处理获取的用户信息
 3      * @author ReCwert
 4      *
 5      */
 6     private static class MyWeiboHandleUserInfoListener implements RequestListener{
 7 
 8         @Override
 9         public void onComplete(String result) {
10             // TODO Auto-generated method stub
11             JSONObject userData = JSON.parseObject(result);
12             nickName = userData.getString("name");
13             headUrl = userData.getString("profile_image_url");
14             sex = userData.getString("gender");
15             Log.i(TAG, "uid = " + uid );
16             Log.i(TAG, "nickName = " + nickName);
17             Log.i(TAG, "headUrl = " + headUrl );
18             Log.i(TAG, "sex = " + sex);
19         }
20 
21         @Override
22         public void onWeiboException(WeiboException exception) {
23             // TODO Auto-generated method stub
24             exception.printStackTrace();
25         }
26         
27     }

 

可以看到,我们在第一个回调中只是获得了access_token,我们需要根据这个access_token去获取跟多的信息,比如去请求userid 和userinfo

1 private static final String WEIBO_USERID_URL = "https://api.weibo.com/oauth2/get_token_info";
2 private static final String WEIBO_USERINFO_URL = "https://api.weibo.com/2/users/show.json";

当然,更多的信息获取的url在sina官方能够查看到
http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI#OAuth2

 

Android第三方授权(新浪微博篇)

标签:

原文地址:http://www.cnblogs.com/recwert/p/4437917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!