Detailed text description: custom menu jump page for WeChat public account development and getting user information instances

Source: Internet
Author: User
Tags oauth openid
This article mainly introduces the custom menu jump page for public account development and obtains detailed information about user information instances, for more information, see the next article. This article mainly introduces the custom menu jump page for public account development and obtains detailed information about user information instances. For more information, see

Custom menu for public account development

Please read this article before proceeding with configuration development

Go to the platform developer documentation to read the "webpage authorization to obtain basic user information" interface description

In the development of a public account, a menu is often defined, and the user clicks the menu to enter the user's personal center function, which is usually applied to member services in various public accounts.

In the custom menu, how does one navigate to the Personal Center page?

First, you need to obtain the user's openid by clicking the user. Instead, you must dynamically bind the user's openid in the menu to obtain the user's openid through the user's click jump, or fill in the provided link in the navigation URL of the menu. two link types are officially provided.

One is the link where Scope is snsapi_base.

https://open.weixin.qq.com/connect/oauth2/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 where Scope is snsapi_userinfo.

https://open.weixin.qq.com/connect/oauth2/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 (the authorization page is not displayed, jump directly, only the user's openid can be obtained), snsapi_userinfo (the authorization page is displayed, and the nickname, gender, and location can be obtained through openid. In addition, users can obtain information even if they do not pay attention to it)

Many online statements use the link url directly as the view type url in the custom menu (you need to configure the webpage authorization callback domain name and appid when entering the url ), I tried this method but it was not successful.

{"Type": "view", "name": "Member Center", "url": "https://open.weixin.qq.com/connect/oauth2/authorize? Appid = your appid & redirect_uri = Do you configure the address for receiving authentication? Response_type = code & scope = snsapi_base & state = 1 # wechat_redirect "},

An error occurred while creating the menu.

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

I tried urlEncode for the later address, and the error is the same.

Then I thought of a solution.

Enter your own url in the custom menu, redirect the user to the snsapi_base url in the entered url, and then configure and obtain the user's openid and other information in snsapi_base, the last page to jump to is the usual Member Center page.

The procedure is as follows:

Please refer to the code

{ "type":"view",

"Name": "Member Center ",

"Url": "http: // configured url/redirect "}

The user jumps

Http: // configured URL/redirect

Call the redirection once in the processing method.

// Configure @ Controller @ RequestMapping ("/wechat") public class WeChatController {@ RequestMapping (value = "/redirect", method = RequestMethod. GET) public String weixinRedirect (HttpServletRequest request, HttpServletResponse response) {return "redirect: https://open.weixin.qq.com/connect/oauth2/authorize? Appid = your appid & redirect_uri = your server processing address? Response_type = code & scope = snsapi_base & state = 1 & connect_redirect = 1 # wechat_redirect ";}}

The server will redirect the authentication to your server's processing address, that is, the above

Redirect_uri = address in the address processed by your server

The configuration here is as follows:

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"; // The access_token contains the openid String URL =" https://api.weixin.qq.com/sns/oauth2/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 that simulates sending http requests. String jsonStr = URLConnectionHelper. sendGet (URL); // System. out. println (jsonStr); // out. print (jsonStr); JSONObject jsonObj = new JSONObject (jsonStr); String openid = jsonObj. get ("openid "). toString (); // with the user's opendi, the user's information can be obtained. // The address is https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN // After obtaining the user information, return to a page model. addAttribute ("user", wechatUser); return "vip/userInfo ";}

The effect is as follows:

The above is the detailed description of the public account development custom menu jump page and get the details of the user information instance. For more information, see other related articles on php Chinese network!

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.