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

Example Analysis of Wechat official account developing Custom menu Jump Page and obtaining user Information

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of developing a custom menu jump page and obtaining user information on the official Wechat account. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Develop custom menus on Wechat official account

Please finish reading this article before configuration development

Please go to the Wechat platform developer's documentation to read the interface description of "Web authorization to obtain basic user information".

In the development of Wechat public accounts, it is often necessary to define a menu, and then the user clicks on the menu to enter the user's personal center, which is usually applied to the member services in various public accounts.

How to navigate the user to the personal Center page in the Wechat custom menu?

First, you need to obtain the user's openid through the user's click, while to obtain the user's openid through the user's click, you must dynamically bind the user's openid in the menu, or fill in the link provided by Wechat in the redirect URL of the menu. Officially, there are two link types.

One is the link whose Scope is snsapi_base.

Https://open.weixin.qq.com/connect/oauth3/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect

The other is the link whose Scope is snsapi_userinfo.

Https://open.weixin.qq.com/connect/oauth3/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

The differences between the two links are as follows

Application authorization scope, snsapi_base (do not pop up authorization page, jump directly, can only get user openid), snsapi_userinfo (pop-up authorization page, you can get nickname, gender, location through openid. And, even without attention, the information can be obtained as long as the user is authorized)

Many people say on the Internet that the linked url is directly used as the url in the view type in the custom menu of Wechat (you need to configure the web page authorization callback domain name and appid when filling in url). I tried this method, but it was not successful.

{"type": "view", "name": "member Center", "url": "https://open.weixin.qq.com/connect/oauth3/authorize?appid= your appid&redirect_uri=, which address do you configure to receive Wechat certification? response_type=code&scope=snsapi_base&state=1#wechat_redirect"}

The result returned is that the menu creation failed

Failed to create menu errcode: {40033} errmsg: {invalid charset. Please check your request, if include\ uxxxx will create fail! Hint: [91..gA0792vr23]}

I tried to urlEncode the following address, but it was the same error.

And then I thought of a way.

Fill in your own url in the custom menu, redirect the user to the url of snsapi_base in the filled url, then configure in snsapi_base to obtain the user's openid and other user information, and finally jump to a page, that is, the usual member center page.

The process is as follows

Please look at the code

{"type": "view"

"name": "member Center"

"url": "URL of http:// configuration / redirect"}

In which the user is redirected to the

URL of http:// configuration / redirect

Then call a redirect in the processing method.

/ / configuration on class @ Controller@RequestMapping ("/ wechat") public class WeChatController {@ RequestMapping (value = "/ redirect", method = RequestMethod.GET) public String weixinRedirect (HttpServletRequest request, HttpServletResponse response) {return "redirect: appid&redirect_uri= your server processing address? response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect";}}

The server will jump Wechat authentication to your server processing address, that is, above.

Redirect_uri= your server processes the address in the address

This is configured as

Your server address / oauth

The code is as follows

@ RequestMapping (value = "/ oauth", method = RequestMethod.GET) public String weixinOAuth (HttpServletRequest request, HttpServletResponse response, Model model) {/ / get code String CODE = request.getParameter ("code"); String APPID = "your APPID"; String SECRET = "your SECRET" / / in exchange for access_token, which contains openid String URL = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code".replace("APPID", APPID). Replace (" SECRET ", SECRET). Replace (" CODE ", CODE); / / URLConnectionHelper is a class String jsonStr = URLConnectionHelper.sendGet (URL) that simulates sending http requests; / / System.out.println (jsonStr) / / out.print (jsonStr); JSONObject jsonObj = new JSONObject (jsonStr); String openid = jsonObj.get ("openid"). ToString (); / / with the user's opendi, you can get the user's information / / address is https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN / / get the user's information and return to a page model.addAttribute ("user", wechatUser) Return "vip/userInfo";}

The effect is as follows

And in this way, when users open it with other browsers, it will make mistakes, which ensures that it can only be used in Wechat and ensures security. And the address bar will not expose other users' personal information.

This is the end of the article on "example analysis of developing a custom menu jump page and obtaining user information on the official Wechat account". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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