Android 第三方授权(微信篇)

0.申请开发者:

https://open.weixin.qq.com/cgi-bin/frame?t=home/app_tmpl&lang=zh_CN

1.下载sdk包:

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN

2.导入sdk:这一步就不说了

3.初始化IWXAPI:

1 IWXAPI api = WXAPIFactory.createWXAPI(this, Constants.WEIXIN_APP_ID, true);

4.发送认证请求:

 1         if( !api.isWXAppInstalled()){
 2             Toast.makeText(context, "请先安装微信应用", Toast.LENGTH_SHORT).show();
 3             return false;
 4         }
 5         if( !api.isWXAppSupportAPI() ){
 6             Toast.makeText(context, "请先更新微信应用", Toast.LENGTH_SHORT).show();
 7             return false;
 8         }
 9         api.registerApp(Constant.WX_APP_ID);
10         final SendAuth.Req req = new SendAuth.Req();
11         req.scope = Constant.WX_APP_SCOPE;
12         req.state = Constant.WX_APP_STATE;
13         api.sendReq(req);

5.第4步会调起微信客户端让用户进行授权,用户进行操作后会调起***.***.***.wxapi.WXEntryActivity 这个类, ***.***.***是应用包名,这个WXEntryActivity需要继承Activity并且实现IWXAPIEventHandler接口,当然,这个WXEntryActivity也需要在清单文件中注册

 1 public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
 2 
 3     private static String TAG = "WXEntryActivity";
 4     private IWXAPI api;
 5     public static BaseResp mResp = null;
 6 
 7     // 是否有新的认证请求
 8     public static boolean hasNewAuth = false;
 9     private TextView mTvCode;
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         Log.i(TAG, "onCreate<-----------------------------");
15         mTvCode = new TextView(this);
16         mTvCode.setText("no code");
17         setContentView(mTvCode);
18         api = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID, false);
19         api.handleIntent(getIntent(), this);
20         Log.i(TAG, "onCreate----------------------------->");
21 
22     }
23 
24     @Override
25     protected void onNewIntent(Intent intent) {
26         super.onNewIntent(intent);
27         setIntent(intent);
28         api.handleIntent(intent, this);
29         finish();
30     }
31 
32     @Override
33     public void onReq(BaseReq arg0) {
34         // TODO Auto-generated method stub
35         Log.i(TAG, "onReq<------------------------------");
36     }
37 
38          /**
39           *认证后会回调该方法
40           */
41     @Override
42     public void onResp(BaseResp resp) {
43         // TODO Auto-generated method stub
44         String code = ((SendAuth.Resp) resp).code;
45                //获取code后需要去获取access_token
46     }
47 }        

获取code之后需要继续用code去获取access_token

 1 //获取微信登陆Token凭证
 2                     Request requestToken = new Request.Builder().url(tokenRequestUrl).build();
 3                     response = httpClient.newCall(requestToken).execute();
 4                     String tokenResponse = response.body().string();
 5                     Log.i(TAG, "tokenResponse = " + tokenResponse);
 6                     weChatTokenDo = JSON.parseObject(tokenResponse, WeiXinTokenDO.class);
 7                     
 8                     //获取微信个人信息
 9                     String userInfoRequest = getUserInfoRequest(weChatTokenDo.getAccess_token(), weChatTokenDo.getOpenid());
10                     Request requestUserInfo = new Request.Builder().url(userInfoRequest).build();
11                     response = httpClient.newCall(requestUserInfo).execute();
12                     String userInfoResponse = response.body().string();
13                     Log.i(TAG, "userInfo = " + userInfoResponse);
14                     String openid = JSON.parseObject    (userInfoResponse).getString("openid");
15                     Log.i(TAG, "微信登陆数据获取结束");
16                                          
1     private static String GET_REQUEST_ACCESS_TOKEN = 
2             "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
3     
4     private static String GET_REQUEST_USER_INFO = 
5             "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
 1     /**
 2      * 获取根据CODE得到的微信授权Token的Get请求地址
 3      * @param code
 4      * @return
 5      */
 6     private static String getTokenRequest(String code ){
 7         String tokenRequest = GET_REQUEST_ACCESS_TOKEN.replace("APPID", Constants.WEIXIN_APP_ID).
 8                 replace("SECRET", Constants.WEIXIN_APP_SECRET).
 9                 replace("CODE",code);
10         return tokenRequest;
11     }

 

至此,获取到的userInfoResponse字符串就是保存了用户个人信息的json串,然后里面都有什么内容可去微信官网看,自己解析操作
另外,别忘记在清单文件中注册

1 <!-- 微信登陆回调 -->
2         <activity
3             android:name"***.***.***.wxapi.WXEntryActivity"
4             android:label="@string/app_name"
5             android:exported="true"/>

 

Android 第三方授权(微信篇)

上一篇:通过微信分享链接,后面会被加上from=singlemessage&isappinstalled=1可能导致网页打不开


下一篇:关于H5在微信浏览器内自动转格式,导致不能正常打开的问题