>  기사  >  백엔드 개발  >  javascript - 微信网页扫码登录不能获取用户信息

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

WBOY
WBOY원래의
2016-06-06 20:12:041105검색

<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
明明有问题。

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.