Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the authorization of the official account OAuth2.0 of Wechat

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the Wechat official account OAuth2.0 web page authorization problem". In the daily operation, I believe many people have doubts about how to understand the Wechat official account OAuth2.0 web page authorization problem. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to understand the Wechat official account OAuth2.0 web page authorization problem". Next, please follow the editor to study!

1. Modify the basic information of web page authorization. Open the Wechat public platform.

Write the URL of the page you want to authorize in this place.

two。 I'm just testing this function, so I wrote a test page directly, and I created a new ceshi.html in the root directory of the site I'm going to test.

And then operate it in his controller.

1) first of all, the links to the shared, that is, authorized pages should be written correctly.

Then url:

Https://open.weixin.qq.com/connect/oauth3/authorize?appid=wx444444444444&redirect_uri=http://www.erdangjiade.com/Admin/Wx/ceshi.html&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect

2) use code to obtain access_token

I operate it directly on the controller of the web page (that is, authorized) that I tested.

Code:

$code=$_GET ['code']; dump ($code); $url= "https://api.weixin.qq.com/sns/oauth3/access_token?appid=wxd1caae7&secret=0a72c866233ab4cf6f1ad6d&code=".$code."&grant_type=authorization_code";//var_dump($url);$resInfo = https_request ($url,true); $userLists=json_decode ($resInfo,true); dump ($userLists)

You can agree to the $userLists written here.

Then, if it is correct, you can get access_token. Because of the timeliness of access_token, you can replace access_token with refresh_token. I didn't write that step because I didn't think I needed it.

If there is a mistake in the exchange, {"errcode": 40029, "errmsg": "invalid code"} will be displayed.

3) use openid and access_token to obtain basic information such as profile pictures and nicknames.

Code:

$access_token=$userLists ['access_token']; $openid=$userLists [' openid']; $userClick=M ('maiclub_userclick'); if ($userClick- > where ("openid=' {$openid}'")-> find ()) {/ / echo 1111;} else {$urlInfo= "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid;$resltInfo=https_request($urlInfo,true);$userInfo=json_decode($resltInfo,true);$userClick->add($userInfo);}dump($userInfo);// dump ($access_token) $this- > display ()

Dump ($userInfo); you can print out the basic information of the user, and then just enter it into the library.

There is another problem here, that is, a function https_request that I use here.

His source code in function:

Function https_request ($url, $data = null) {$curl = curl_init (); curl_setopt ($curl, CURLOPT_URL, $url); curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! empty ($data)) {curl_setopt ($curl, CURLOPT_POST, 1); curl_setopt ($curl, CURLOPT_POSTFIELDS, $data);} curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec ($curl); curl_close ($curl); curl $return}

There should be no problem in this way, that is, the whole content of the authorization.

This is the end of the study on "how to understand the authorization of OAuth2.0 web pages on Wechat official account". I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report