• [转] Android进阶——安卓接入微信,获取OpenID


    PS: sendAuthRequest拿到code,通过code拿到access_token和openId,access_token可以拿到用户的信息

    http://blog.csdn.net/haovip123/article/details/50503176

    需求:接入微信支付,需要获取 OpenID。

    微信开放平台上面对大多数步骤都有详细的介绍。但是……,还是自己梳理一下吧。

    1.申请AppID。

        (微信支付或微信登录等功能需要进行开发者资质认证,准备好300大洋)

    2.下载最新SDK。

    3.导入jar包,并配置权限。

    4.代码实现

      ①  注册到微信
    1. // 通过WXAPIFactory工厂,获取IWXAPI的实例  
    2. api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, true);  
    3. api.handleIntent(getIntent(), this);  
    4. // 将该app注册到微信  
    5. api.registerApp(Constants.APP_ID);  
       ②  发送请求
    1. final SendAuth.Req req = new SendAuth.Req();  
    2. req.scope = "snsapi_userinfo";  
    3. req.state = "wechat_sdk_demo_test";  
    4. api.sendReq(req);  
       ③ 接受微信请求(获取code值)
    1. // 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法  
    2. @Override  
    3. public void onResp(BaseResp resp) {  
    4.     int result = 0;  
    5.     SendAuth.Resp re = ((SendAuth.Resp) resp);  
    6.     String code = re.code;  
    7.   
    8.     switch (resp.errCode) {  
    9.         case BaseResp.ErrCode.ERR_OK:  
    10.             result = R.string.errcode_success;  
    11.             getOpenID(code);  
    12.   
    13.             break;  
    14.         case BaseResp.ErrCode.ERR_USER_CANCEL:  
    15.             result = R.string.errcode_cancel;  
    16.             break;  
    17.         case BaseResp.ErrCode.ERR_AUTH_DENIED:  
    18.             result = R.string.errcode_deny;  
    19.             break;  
    20.         default:  
    21.             result = R.string.errcode_unknown;  
    22.             break;  
    23.     }  
    24.   
    25.     Toast.makeText(this, result, Toast.LENGTH_LONG).show();  
    26.     Toast.makeText(this, code, Toast.LENGTH_LONG).show();  
    27. }  
       ④ 通过code获取access_token,code等数据
    1. private void getOpenID(String code) {  
    2.     // APP_ID和APP_Secret在微信开发平台添加应用的时候会生成,grant_type 用默认的"authorization_code"即可.  
    3.     String urlStr = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+Constants.APP_ID+"&secret="+Constants.APP_Secret+  
    4.             "&code="+code+"&grant_type=authorization_code";  
    5.   
    6.     HttpUtils http = new HttpUtils();  
    7.     // 设置超时时间  
    8.     //        http.configCurrentHttpCacheExpiry(1000 * 10);  
    9.     http.send(HttpRequest.HttpMethod.GET, urlStr, null,  
    10.             new RequestCallBack<String>() {  
    11.                 // 接口回调  
    12.                 @Override  
    13.                 public void onSuccess(ResponseInfo<String> info) {  
    14.                     System.out.println("返回的json字符串:" + info.result);  
    15.                     Toast.makeText(getApplicationContext(), info.result, Toast.LENGTH_SHORT).show();  
    16.   
    17.                     JSONObject obj = null;  
    18.                     try {  
    19.                         obj = new JSONObject(info.result);  
    20.                         //toast  OpenID  
    21.                         Toast.makeText(getApplicationContext(), obj.getString("openid"), Toast.LENGTH_LONG).show();  
    22.   
    23.                     } catch (JSONException e) {  
    24.                         e.printStackTrace();  
    25.                     }  
    26.                 }  
    27.   
    28.                 @Override  
    29.                 public void onFailure(com.lidroid.xutils.exception.HttpException e, String s) {  
    30.   
    31.                 }  
    32.             });  
    33. }  


    1.下载的SDK一定要是最新的,旧一点的SDK里面在获取code的时候没有 .code属性,比如官方demo中万年不变的sdk就害的我很惨。

    签名生成工具链接。
  • 相关阅读:
    Codeforces 877 C. Slava and tanks
    Codeforces 877 D. Olya and Energy Drinks
    2017 10.25 NOIP模拟赛
    2017 国庆湖南 Day1
    UVA 12113 Overlapping Squares
    学大伟业 国庆Day2
    51nod 1629 B君的圆锥
    51nod 1381 硬币游戏
    [JSOI2010]满汉全席
    学大伟业 2017 国庆 Day1
  • 原文地址:https://www.cnblogs.com/qiangxia/p/5696271.html
Copyright © 2020-2023  润新知