如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。
请求方法
http:GET(请使用https协议) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
//-------------------------------------------------------------------------上面是微信给的文档。链接地址为:
https://mp.weixin.qq.com/wiki
老是报"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest hint: [8YZU.A0663vr29!]"}
最后才发现,这短短的一句话,微信弄出来2个错误。
错误1:https:请求错误。正确的请求是下面的这个。看看,差多远。
[php] view plain copy
- <strong> // $url4="https://api.weixin.qq.com/sns/userinfo?access_token=".$accessToken."&openid=".$response2['openid']."&lang=zh_CN";
- $url4="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$response2['access_token']."&openid=".$response2['openid']."&lang=zh_CN";</strong>
错误2:这链接里面的access_token明明写的是网页授权token.也就是第二部获取的token,结果呢,却要用基础access_token才能查询出来。
我估计是他们接口调整了。结果文档没有跟上,再加上版本迭代。总之他们也不管。让程序员自己挠头把!!!!!
MLGB,微信的程序员,你能认真的不,能死啊!!!!!
//---------------后期补录。。。。。。。。。。理解错误:貌似2套获取微信用户信息的接口。
- <strong>api.weixin.qq.com/cgi-bin/user/info 这个接口 跟 基础access_token配合使用</strong>
- <strong></strong><pre name="code" class="php"><strong>https://api.weixin.qq.com/sns/userinfo这个接口跟 网页授权access_token配合使用</strong></pre><br>
- <p></p>
- <pre></pre>
- <p></p>
- <p style="margin-top:0px; margin-bottom:0px; padding-bottom:14px; font-size:14px; color:rgb(34,34,34); font-family:"Helvetica Neue","Hiragino Sans GB","Microsoft YaHei",黑体,Arial,sans-serif">
- <span style="color:rgb(34,34,34); font-family:"Helvetica Neue","Hiragino Sans GB","Microsoft YaHei",黑体,Arial,sans-serif; font-size:14px; background-color:rgb(253,234,218)"><br>
- </span></p>
- <p style="margin-top:0px; margin-bottom:0px; padding-bottom:14px; font-size:14px; color:rgb(34,34,34); font-family:"Helvetica Neue","Hiragino Sans GB","Microsoft YaHei",黑体,Arial,sans-serif">
- <span style="color:rgb(34,34,34); font-family:"Helvetica Neue","Hiragino Sans GB","Microsoft YaHei",黑体,Arial,sans-serif; font-size:14px; background-color:rgb(253,234,218)"></span></p>
- <h1 class="postTitle" style="margin:10px 0px; padding:0px; border-bottom:1px solid rgb(221,221,221); font-size:14px; font-family:verdana,Arial,Helvetica,sans-serif">
- <a target="_blank" id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/sxmny/articles/4969119.html" style="margin:0px; padding:0px; text-decoration:none; color:rgb(57,154,178)">微信获取用户信息的两个接口和两个ACCESS_TOKEN</a></h1>
- <div class="clear" style="margin:0px; padding:0px; clear:both; color:rgb(51,51,51); font-family:verdana,Arial,Helvetica,sans-serif; font-size:13.3333px">
- </div>
- <div class="postBody" style="margin:0px; padding:0px; font-size:14px; line-height:1.8; color:rgb(51,51,51); font-family:verdana,Arial,Helvetica,sans-serif">
- <div id="cnblogs_post_body" style="margin:0px 0px 20px; padding:0px; word-break:break-word">
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 有一段时间没有搞微信开发了 ,今天突然要改一下程序! 回头一看 微信的帮助文档太tm的稀烂的,太难懂了,这做个笔记以后看着方便</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 微信有2个ACCESS_TOKEN,</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 1,基础接口的token 获取接口是 </p>
- <pre style="margin-top:0px; margin-bottom:0px; padding:0px; white-space:pre-wrap; word-wrap:break-word">https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET<a target="_blank" class="external free" href="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET" rel="nofollow" style="margin:0px; padding:0px; text-decoration:none; color:rgb(0,0,0); border-bottom:1px dotted rgb(51,51,51)"><br style="margin:0px; padding:0px"></a></pre>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 2,用户网页授权access_token 获取接口地址是</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 网页授权access_token 需要通过code去获取</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> code是怎么来的,是通过调用下面接口来获取的</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 注意这个接口中有个参数scope 默认有2个值snsapi_base和snsapi_userinfo,这个接口会根据scope 来生成不同的code并且获取不同作用的access_token ,不管scope传什么值都能在得到对应access_token的同时得到open_id, 如果你只需要得到opend_id 那使用snsapi_base参数到此结束了,如果需要获取用户的其他信息比如
- 昵称 地址 就要snsapi_userinfo 会弹出授权</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> </p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 3 怎么获取用户信息那就调用下面接口</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 很明显这个接口中的access_token是第二步获取code的时候scope 参数传snsapi_userinfo来换取的access_token</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> </p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px"> 4 微信还有一个获取用户基本信息的接口 但是 这个接口需要你关注了公众号</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px">https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN (此接口的access_token 是接口基础调用access_token 不是网页授权access_token)</p>
- <p style="margin:10px auto; padding-top:0px; padding-bottom:0px">微信的解释:是在用户和公众号产生消息交互或关注后事件推送后,才能根据用户OpenID来获取用户基本信息。这个接口,包括其他微信接口,都是需要该用户(即openid)关注了公众号后,才能调用成功的。</p>
- </div>
- </div>
- <br>
- <p></p>
- <p style="margin-top:0px; margin-bottom:0px; padding-bottom:14px; font-size:14px; color:rgb(34,34,34); font-family:"Helvetica Neue","Hiragino Sans GB","Microsoft YaHei",黑体,Arial,sans-serif">
- <span style="color:rgb(34,34,34); font-family:"Helvetica Neue","Hiragino Sans GB","Microsoft YaHei",黑体,Arial,sans-serif; font-size:14px; background-color:rgb(253,234,218)"><br>
- </span></p>
- <div style="color:rgb(34,34,34); font-family:"Helvetica Neue","Hiragino Sans GB","Microsoft YaHei",黑体,Arial,sans-serif; font-size:14px; margin-bottom:14px; padding:10px 16px; background-color:rgb(253,234,218)">
- <p style="margin-top:0px; margin-bottom:0px; padding-bottom:14px"><br>
- </p>
- </div>