Micro-Credit public number-Get user information (Web page authorization get) steps _javascript Tips

Source: Internet
Author: User
Tags openid

Develop official documents based on the micro-credit public number:

To obtain user information step as follows:

1 First step: User consent authorization, get code
2 Second step: Through code in exchange for Web page authorization Access_token
3 Step Three: Refresh Access_token (if required)
4 Fourth Step: Pull user information (need scope for snsapi_userinfo)

1 Getting Code

In order to ensure that the micro-credit public account has the authorization scope (scope parameter) of the authority (the service number after the advanced interface, the default has the scope of the Snsapi_base and Snsapi_userinfo), guide the attention to open the following page:

Https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_ Type=code&scope=scope&state=state#wechat_redirect

If you are prompted for "This link cannot be accessed", check that the parameter is filled in incorrectly and that you have the authorization scope permission for the scope parameter.

Especially note: Because the authorization operation security level is high, so when the authorization request, the micro-letter will do the authorization link to do strong match check, if the link parameter order is not correct, the authorization page will not be normal access

which

AppID -Unique identification of the public number
redirect_uri -Jump URL
SCOPE- value is snsapi_base (does not eject authorization page, direct jump, can only obtain user OpenID) or Snsapi_userinfo (pop-up authorization page, can get nickname, sex, location through OpenID). And, even in the event of not paying attention, can obtain the information as long as the user authorizes.
State-Developers can customize parameter values for filling a-za-z0-9

2 through code in exchange for Web page authorization Access_token

If the user agrees to authorize, the page jumps to redirect_uri/?code=code&state=state.
State is where the state parameter is passed.

Implementation code:

<code class= "Hljs php" > $code = I (' Get.code ');
if (empty ($code)) {
   //todo access
   exit (' </code> ');
 } else{//Authorization after operation}

Here we can get code to use for subsequent acquisition access_token.

After obtaining the code, request the following link to obtain Access_token:

Https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type= Authorization_code

AppID -Unique identification of the public number
Secret -Key
code -the code returned above
Grant_type -value is Authorization_code

Implementation code:

<code class= "Hljs bash" > $url = ' https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential& Appid= '. C (' WeChat. AppID '). ' &secret= '. C (' WeChat. Appsecret ');
$str = file_get_contents ($url);
$str = Json_decode ($str, true);
$access _token = $str [' Access_token '];</code>

Here access_token can do cache processing, avoid causing frequent gain
Implementing code, taking the TP framework as an example:

<code class= "Hljs php" > $access _token = S (' Access_token ');
if (Empty ($access _token)) {
  $url = ' https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential& Appid= '. C (' WeChat. AppID '). ' &secret= '. C (' WeChat. Appsecret ');
  $str = file_get_contents ($url);
  $str = Json_decode ($str, true);
  $access _token = $str [' Access_token '];
  S (' Access_token ', $access _token, 3600);
} </code>

After acquiring Access_token, will also return to OpenID (user only identification), the micro-letter Official document explanation is: The user unique identification, please note, when not concerned about the public number, the user visits the public number webpage, also can produce a user and the public number only OpenID

OpenID is the only identification of micro-trust users, if the user is not the first time, you can query the database after the OpenID is bound to this OpenID users, then do not need to regain user data, directly access to the database user_id set session, direct access to

3 Third step ignore, only in need of time to regain Access_token only

4 Pull user information (need scope for snsapi_userinfo)

In the database without this micro-signal user's binding set, which is equivalent to the user's first visit to the landing, the fourth step to obtain user information (in the case of user authorization, Web page authorization scope for Snsapi_userinfo, then developers can pass Access_ Token and OpenID pull user information), and then create user in the background and bind this micro

Credit accounts (via OpenID)

Request method

Http:get (please use HTTPS protocol) https://api.weixin.qq.com/sns/userinfo?access_token=access_token&openid=openid& Lang=zh_cn

Access_token-the Access_token obtained above
OpenID-Public Number unique identification

Implementation code:

<code class= "Hljs php" > $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token= $access _token& openid= $openid &lang=zh_cn ";
$str = file_get_contents ($url);
$str = Json_decode ($str, true);
if (Empty ($str [' Errcode ']) {
  //create user and bind OpenID
}else{
  //error handling
}</code>

The above is the micro-credit public number to obtain user information specific steps.

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.