[轉] Android進階——安卓接入微信,獲取OpenID

PS: sendAuthRequest拿到code,經過code拿到access_token和openId,access_token能夠拿到用戶的信息java

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

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

 

 

 

微信開放平臺上面對大多數步驟都有詳細的介紹。可是……,仍是本身梳理一下吧。

1.申請AppID。

    (微信支付或微信登陸等功能須要進行開發者資質認證,準備好300大洋)

2.下載最新SDK。

3.導入jar包,並配置權限。

4.代碼實現

  ①  註冊到微信
[java] view plain copy 在CODE上查看代碼片派生到個人代碼片
  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);  
   ②  發送請求
[java] view plain copy 在CODE上查看代碼片派生到個人代碼片
  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值)
[java] view plain copy 在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等數據
[java] view plain copy 在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就害的我很慘。

簽名生成工具連接。
相關文章
相關標籤/搜索