微信 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
2016-11-19 11:47
477 查看
{"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
声明:是已认证的服务号这个问题已解决!!
前置说明
网页授权获取用户基本信息:两种 scope 域https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state=ok#wechat_redirect
snsapi_base 不需要用户点同意,直接跳转到授权后的页面,只能用于获取openid,不能获取用户基本信息
snsapi_userinfo 会征求用户同意,授权后,可以获取用户基本信息
为了方便大家理解,把接口返回的数据展示下吧
{"access_token":"YNTxefBAfNW565Q4MAI..................j-hLNzin9Vq6BnvcvJ8NKHdBSRhkLjHQliLiNQc",
"expires_in":7200,
"refresh_token":"EYmfmvUg0PGtOWiLpb..................T8O2UyKL95_Da23fLV-nYFDgnt14Ljl_x_zCMY",
"openid":"oYbGRs8MmU6bESxXcX2Z0rFRivjQ",
"scope":"snsapi_base"
}
{
"access_token":"IdFXvRMq3J6vXUcZ0iQ..................G1xbkGJEnbnSKQ4G0nyQ5vmNSKF4a4DzGu64",
"expires_in":7200,
"refresh_token":"J8ot4JWphxwvyAY3I9r..................krET6P-8dot_-_tQfFoLUQI3EI-NT-I-degNRGumA",
"openid":"oYbGRs8MmU6bESxXcX2Z0rFRivjQ",
"scope":"snsapi_userinfo"
}
问题再现
1 首次使用 scope=snsapi_base 进行网页授权2 拿到 code 后调用接口 https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code
3 根据上一步获取的 openid 和 access_token 调用接口 https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN 获取用户基本信息
4 获取失败:返回 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
失败原因
scope=snsapi_base 时,只能用于获取openid,不能获取用户的基本信息(获取用户信息,需要用户同意。scope=snspai_base 授权时,并没有征求用户同意授权这一操作)问题来了
(有时候成功,有时候失败)为什么有的时候(或者另外一个公众号),按上面的步骤,能成功获取到用户的基本信息?
“48001,api unauthorized”之所以让很多人迷惑不解,就在于这个问题
先直接上解决方式,再说原因
使用 scope=snsapi_userinfo 进行一次网页授权,步骤跟上面【问题再现】一样,成功获取到用户的微信基本信息。再使用 scope=snsapi_base 重复一遍【问题再现】,这次成功了!!!
原因
上面的实验其实已经说明原因了,怕有些人一下子理解不了,所以第一次使用 scope=snsapi_base,这是不需要经过用户同意的授权,不能获取用户的基本信息
第二次使用 scope=snsapi_userinfo,这是需要用户点击同意的授权,能获取到用户的基本信息
使用 scope=snsapi_userinfo,用户同意授权后,在一定时间内,不需要用户的同意,也可以获取用户基本信息(此时使用 scope=snsapi_base 也能获取到用户基本信息:甚至参数openid的值随便填都没关系,可以是空格,但不能为空)
注意
使用 scope=snsapi_userinfo,用户同意授权后,access_token 的有效期是7200秒(两小时)。access_token 失效后,可以使用 refresh_token 调用接口 https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1} 重新获取 access_token(有效期7200秒)
(终于知道 refresh_token 是干嘛用的了吧)
refresh_token 的有效期是30天,即用户同意授权后,在30天内,不再需要用户同意授权,就可以获取用户基本信息
打完收工,请叫我 Alexander Mifan
相关文章推荐
- 微信 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
- 调用 微信接口报错 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
- 微信登录提示48001,{"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
- 调用 微信接口报错 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
- 微信 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
- {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"} 声明:是已认证的服务号
- {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
- 微信 {"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"}
- 微信登陆问题{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: xxxx]"
- ERR_GET_SESSION_KEY↵{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: JlvASa0355th54 ]"}
- 微信公众号返回错误{"errcode":40090,"errmsg":"invalid request source (bad client ip), hints: [ req_id: koZY704
- 微信公众号问题:{"errcode":40125,"errmsg":"invalid appsecret, view more at http:\/\/t.cn\/LOEdzVq, hints: [ req_id: kL8J90219sg58 ]"}
- 微信坑-网页授权登录errcode=48001,errmsg=api unauthorized, hints: [ req_id: Q0188s105 ]
- Java企业微信开发_Exception_01_"errcode":60011,"errmsg":"no privilege to access/modify contact/party/agent "
- 微信上传素材 {"errcode":41005,"errmsg":"media data missing"} 解决方法和思路
- 微信 "errcode": 85005, "errmsg": "appid not bind weapp hint: [tghx1a0557vr18]"
- {"errcode":40097,"errmsg":"invalid args hint: [vjNe7xxxxxx8vr19]"}——记录一次微信错误处理
- 微信高级群发接口 {"errcode":40008,"errmsg":"invalid message type hint: [aRIDBA0726age9]"}
- 微信网页授权获取用户信息 返回{"errcode":40066,"errmsg":"invalid url hint: [FZbxRA0556sz12!]"}错误