SoFunction
Updated on 2025-03-01

WeChat {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}

{"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}

Statement: It is a certified service number

Preliminary instructions

Web page authorization to obtain basic user information: two scope domains

/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state=ok#wechat_redirect

snsapi_base does not require user point consent, and jumps directly to the authorized page. It can only be used to obtain openid and cannot obtain basic user information.

snsapi_userinfo will seek user consent. After authorization, you can obtain user basic information.

To facilitate everyone's understanding, display the data returned by the interface.

{

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

}

The problem reappears

1. Use scope=snsapi_base for the first time to authorize the web page

2. Call the interface after getting the code/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code

3. Call the interface according to the openid and access_token obtained in the previous step/sns/userinfo?access_token={0}&openid={1}&lang=zh_CNGet basic user information

4  Get failed: Return {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}

Reason for failure

When scope=snsapi_base, it can only be used to obtain openid, and cannot obtain the user's basic information (user information is required to obtain user consent. When scope=snspai_base authorization, the user's consent is not solicited for user consent)

The problem is here

(Sometimes succeed, sometimes fail)

Why sometimes (or another official account) can successfully obtain the user's basic information by following the above steps?

The reason why "48001,api unauthorized" puzzles many people is that this problem

First, directly solve the problem, then talk about the reason

Use scope=snsapi_userinfo to perform a web page authorization. The steps are the same as the above [Problem Reappearance], and the user's basic WeChat information is successfully obtained.

Use scope=snsapi_base to repeat [problem reappear] again, and this time it was successful! ! !

reason

The above experiment has actually explained the reason, I am afraid that some people will not understand it all at once, so

The first time using scope=snsapi_base, this is an authorization that does not require user consent and cannot obtain user basic information.

The second time you use scope=snsapi_userinfo, this is an authorization that requires the user to click on the consent, and you can obtain the user's basic information.

Use scope=snsapi_userinfo, after the user agrees to authorization, within a certain period of time, the user's basic information can also be obtained without the user's consent (at this time, use scope=snsapi_base to obtain the user's basic information: it doesn't matter if the value of the parameter openid is filled in casually, it can be a space, but it cannot be empty)

Notice

With scope=snsapi_userinfo, after the user agrees to authorization, the validity period of access_token is 7200 seconds (two hours).

After access_token expires, you can use refresh_token to call the interface/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1} Re-get access_token (valid for 7200 seconds)

(I finally know what refresh_token is used for)

The validity period of refresh_token is 30 days, that is, after the user agrees to authorize, within 30 days, the user's consent is no longer required to authorize, so that the user can obtain basic user information

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.