首頁 > 後端開發 > php教程 > javascript - 微信网页扫码登录不能获取用户信息

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

WBOY
發布: 2016-06-06 20:12:04
原創
1156 人瀏覽過

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

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板