Home  >  Article  >  Backend Development  >  javascript - 微信网页扫码登录不能获取用户信息

javascript - 微信网页扫码登录不能获取用户信息

WBOY
WBOYOriginal
2016-06-06 20:12:041107browse

<code>getToken: function(code) {
        var url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=id&secret=sec&code=" + code +"&grant_type=authorization_code";
        //synchronous GET
        // console.log("url:" + url);
        var result = HTTP.get(url, {timeout:30000});
        // console.log("result:" + result);
        if(result.statusCode==200) {
            var respJson = JSON.parse(result.content);
            console.log("response received.");
            return respJson;
        } else {
            console.log("Response issue: ", result.statusCode);
            var errorJson = JSON.parse(result.content);
            throw new Meteor.Error(result.statusCode, errorJson.error);
        }
    },</code>

已经获取到access_token和openid,调用https://api.weixin.qq.com/sns/authaccess...验证access_token也没问题。但是用access_token和openid获取用户信息总是返回errcode: 40001, errmsg: "invalid credential, access_token is invalid or not latest, hints: [ req_id: vkq6LA0879ns44 ]"。正确的返回是:

<code>{ 
"openid":"OPENID",
"nickname":"NICKNAME",
"sex":1,
"province":"PROVINCE",
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
"privilege":[
"PRIVILEGE1", 
"PRIVILEGE2"
],
"unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"

}
</code>

网页扫码登录的scope只能是snsapi_login,难道是因为scope不是snsapi_userinfo而获取数据失败?
那这样网页扫码登录微信就只能拿到一个openid?

回复内容:

<code>getToken: function(code) {
        var url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=id&secret=sec&code=" + code +"&grant_type=authorization_code";
        //synchronous GET
        // console.log("url:" + url);
        var result = HTTP.get(url, {timeout:30000});
        // console.log("result:" + result);
        if(result.statusCode==200) {
            var respJson = JSON.parse(result.content);
            console.log("response received.");
            return respJson;
        } else {
            console.log("Response issue: ", result.statusCode);
            var errorJson = JSON.parse(result.content);
            throw new Meteor.Error(result.statusCode, errorJson.error);
        }
    },</code>

已经获取到access_token和openid,调用https://api.weixin.qq.com/sns/authaccess...验证access_token也没问题。但是用access_token和openid获取用户信息总是返回errcode: 40001, errmsg: "invalid credential, access_token is invalid or not latest, hints: [ req_id: vkq6LA0879ns44 ]"。正确的返回是:

<code>{ 
"openid":"OPENID",
"nickname":"NICKNAME",
"sex":1,
"province":"PROVINCE",
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
"privilege":[
"PRIVILEGE1", 
"PRIVILEGE2"
],
"unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"

}
</code>

网页扫码登录的scope只能是snsapi_login,难道是因为scope不是snsapi_userinfo而获取数据失败?
那这样网页扫码登录微信就只能拿到一个openid?

access_token is invalid or not latest
明明有问题。

检查下是否正确,是否已过期。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn