欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-10 12:23:37
...
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);
        }
    },

已经获取到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 ]"。正确的返回是:

{ 
"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"

}

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

回复内容:

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);
        }
    },

已经获取到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 ]"。正确的返回是:

{ 
"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"

}

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

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

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