cocos creator game access WeChat login

1. Log in to the WeChat public platform, configure appid, developer password, and IP whitelist in "Development-Basic Configuration"

2. Configure the web authorization domain name in "Development-Interface Authority-Web Service-Web Authorization"

As shown in the figure above, click on the setting as shown in the figure below

According to the precautions, download the file to the directory of the web server that fills in the domain name. I put it in the ...Tomcat 8.5\webapps\ROOT directory. If you are not sure, you can copy multiple copies and put them in each directory you think is suitable.

And modify the port of tomcat, change 8080 to 80. If you modify the port and find that port 80 is occupied, please refer to the article http://blog.csdn.net/qq_33440781/article/details/54310901 for operation.

Set "Business Domain Name" and "JS Interface Security Domain Name" at the same time as shown in the figure below

3. Development and implementation, mainly refer to WeChat official documents

 

1. First configure the callback domain name

 2. Construct the request url as follows:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8888888888888888&redirect_uri=http://mascot.duapp.com/oauth2.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect

In the page URL

scope=snsapi_userinfo 表示应用授权作用域为请求用户信息
★ 如果使用别人的AppID和AppSecret,那么获得的OpenID是那个有高级接口权限的服务号的,这里可以通过消息回复,获取本公众账号下的OpenID,带入回调中,与另一个OpenID进行关联也可以使用开放平台的UnionID功能来得到用户在自己账号下的OpenID 
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8888888888888888&redirect_uri=http://mascot.duapp.com/oauth2.php?userid=oc7tbuPA9BgUCLADib5nB3k2KWWg&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect

Reply the link to the following user, after the user clicks, the application authorization interface pops up

3. The link to the callback page is as follows, the callback url will contain the parameter code

http://mascot.duapp.com/oauth2.php?code=00b788e3b42043c8459a57a8d8ab5d9f&state=1
或者 http://mascot.duapp.com/oauth2.php?userid=oc7tbuPA9BgUCLADib5nB3k2KWWg&code=00b788e3b42043c8459a57a8d8ab5d9f&state=1

4. Use the code in exchange for the authorization access_token of oauth2

The url is as follows:

https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx8888888888888888&secret=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&code=00b788e3b42043c8459a57a8d8ab5d9f&grant_type=authorization_code

Get authorized Access Token:

Copy code

{
    "access_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-G0ZWEE5-uBjBz941EOPqDQy5sS_GCs2z40dnvU99Y5AI1bw2uqN--2jXoBLIM5d6L9RImvm8Vg8cBAiLpWA8Vw",
    "expires_in": 7200,
    "refresh_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-G0ZWEE5-uBjBz941EOPqDQy5sS_GCs2z40dnvU99Y5CZPAwZksiuz_6x_TfkLoXLU7kdKM2232WDXB3Msuzq1A",
    "openid": "oLVPpjqs9BhvzwPj5A-vTYAX3GLc",
    "scope": "snsapi_userinfo,"
}

Copy code

5. Use authorized Access Token to obtain user information

The url is as follows:

https://api.weixin.qq.com/sns/userinfo?access_token=OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-G0ZWEE5-uBjBz941EOPqDQy5sS_GCs2z40dnvU99Y5AI1bw2uqN--2jXoBLIM5d6L9RImvm8Vg8cBAiLpWA8Vw&openid=oLVPpjqs9BhvzwPj5A-vTYAX3GLc

Returns as follows

Copy code

{
    "openid": "oLVPpjqs9BhvzwPj5A-vTYAX3GLc",
    "nickname": "刺猬宝宝",
    "sex": 1,
    "language": "zh_CN",
    "city": "深圳",
    "province": "广东",
    "country": "中国",
    "headimgurl": "http://wx.qlogo.cn/mmopen/utpKYf69VAbCRDRlbUsPsdQN38DoibCkrU6SAMCSNx558eTaLVM8PyM6jlEGzOrH67hyZibIZPXu4BK1XNWzSXB3Cs4qpBBg18/0",
    "privilege": []
}

Copy code

Acquiring user information is complete.

The final user information is as follows

For the detailed process of this method, please refer to  WeChat public platform development (71) OAuth2.0 webpage authorization

This method is suitable,

1. Get the user's information in the circle of friends.

2. Obtain user information on the web page.

3. Obtain user information in the custom menu.

 

Guess you like

Origin blog.csdn.net/wdglhack/article/details/79415005