android 微信第三方登录怎么通过code获取openid?

发布于 2022-08-29 23:34:24 字数 177 浏览 13 评论 0

我已经获取到微信客户端返回的code,但是通过https://api.weixin.qq.com/sns/oauth2/access_toke网址没有获取到openid,网址无效果

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的黑色迷你裙 2022-09-05 23:34:24

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;
}
坐在坟头思考人生 2022-09-05 23:34:24

楼主,跪求你是怎么获取code的?求具体的代码啊。我这个根本无法获取code,反编译之后代码里也没有code这个属性

东京女 2022-09-05 23:34:24

https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&verify=1&lang=zh_CN&token=e86321da7b63085479a8db8e4552b98efcb599be

注意“[3] 接收微信的请求及返回值” 这部分内容,包名和类名要跟文档里的命名的一样才可以

野心澎湃 2022-09-05 23:34:24

//重写onresume()方法

@Override
protected void onResume() {
if (type != null && type.equals("mwx")) {
SharedPreferences settings = getSharedPreferences("setting", 0);
String code = settings.getString("code", null);
if (code != null && !code.equals("")) {
showProgress(true);
getOpenid(code);
}
settings.edit().clear();
settings.edit().commit();
}
super.onResume();
}

// 获取微信用户的openid和access token
public void getOpenid(String code) {
final AsyncHttpClient httpClient = Gl.sharedAsyncClient();
RequestParams params = new RequestParams();
params.put("appid", Constants.wxAPP_ID);
params.put("secret", 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() {

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                JSONObject response) {
            try {
                String opendid = response.getString("openid");
                if (opendid != null && !opendid.equals("")) {
                    openid = response.getString("openid");
                    otherLogin("mwx", opendid);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            super.onSuccess(statusCode, headers, response);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文