我已经获取到微信客户端返回的code,但是通过https://api.weixin.qq.com/sns/oauth2/access_toke网址没有获取到openid,网址无效果
高洛峰2017-04-17 12:02:41
1.登入公用帳號設定OAuth2.0
2.設定選單按鈕URL為OAuth連結
3.頁面後台取得:
public String getopenId() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html");
String code = request.getParameter("code");
String urlstr = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=<appId>&secret=<secret>&code=" + code + "&grant_type=authorization_code";
JSONObject json;
try {
json = JSONObject.fromObject(HTTPTools.postToGetJson(urlstr));
openId = json.getString("openid");
} catch (Exception e) {
// e.printStackTrace();
return "";
}
return openId;
}
ringa_lee2017-04-17 12:02:41
https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&verify=1&lang=zh_CN&token=e86321da7b63085479a8db8e4552b98efcb599be<
注意「[3] 接收微信的請求及回傳值」 這部分內容,包名和類別名稱要跟文件裡的命名的一樣才可以ringa_lee2017-04-17 12:02:41
//重寫onresume()方法
@Override
protected void onResume() {
if (type != null && type.equals("mwx")) {
SharedPreferences 設定 = getSharedPreferences("setting", 0);
String code = settings.getString("code", null);
if (code != null && !code.equals("")) {
顯示進度(true);
getOpenid(代碼);
}
settings.edit().clear();
settings.edit().commit();
}
super.onResume();
}
//取得微信用戶的openid和access token
public void getOpenid(字串碼) {
最終 AsyncHttpClient httpClient = Gla.sharedAsyncClient();
RequestParams params = new RequestParams();
params.put("appid", Constants.wxAPP_ID);
params.put("秘密", Constants.wxAppSecret);
params.put("code", code);
params.put("grant_type", "authorization_code");
String httpurl = "https://api.weixin.qq.com/sns/oauth2/access_token";
httpClient.get(httpurl, params, new JsonHttpResponseHandler() {