Micro-credit public number to develop a custom menu jump page and get a sample of the user information _ basic knowledge

Source: Internet
Author: User
Tags http request oauth openid

Micro-Credit public number development custom menu

Please read this article before configuring the development

Please go to the Micro-trust platform Developer's documentation to read the interface description of "Web page authorization to get user basic information"

In the development of micro-credit public account, there is often a menu defined, then the user clicks on the menu to enter the user's personal center function, usually used in the various public accounts of member services.

How do I navigate the user to the Personal Center page in the micro-mail customization menu?

The first choice is to get user OpenID through user clicks, and the user's click Jump to get user OpenID must be dynamically bound in the menu of the user's OpenID, or in the menu jump URL to fill in the micro-letter provided by the link, the official gave two types of links

One is the snsapi_base link for scope

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 a link to scope 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 difference between the two types of links is as follows

应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)

Many of the online parlance is to link the URL directly as a micro-mail custom menu in the view type of the URL (in the URL is required to configure the page authorization callback domain name and AppID), I tried this approach but not successful

{"Type": "View", "name": "Member Center", "url": "Https://open.weixin.qq.com/connect/oauth2/authorize?appid= your appid& Redirect_uri= you configure to receive a micro-authentication address? Response_type=code&scope=snsapi_base&state=1#wechat_redirect "},

Return result is Create menu failed

创建菜单失败 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, or the same error.

Then I thought of a way

In the custom menu, fill in your own URL, in the URL filled out to redirect the user to the Snsapi_base URL, and then in the Snsapi_base configuration to obtain user OpenID and user other information, and finally jump to a page, that is, the usual Member Center page.

Process is as follows

Please look at the code

{ "type":"view",

"name":"会员中心",

"url":"http://配置的网址/redirect"}

Where the user jumps through the URL to the

http://configuration URL/redirect

You can then call a redirect in the processing method

Class
@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 jump to your server processing address, which is

redirect_uri=你的服务器处理地址中的地址

This is configured to

你的服务器地址/oauth

The code is as follows

@RequestMapping (value = "/oauth", method = requestmethod.get) public String Weixinoauth (httpservletrequest request, HTT
    Pservletresponse response, model model) {//Get code String Code = request.getparameter ("code");
    String APPID = "Your APPID";
    String SECRET = "Your SECRET"; In exchange for Access_token which contained an 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 String jsonstr = Urlconnectionhelper.sendget (URL) that simulates sending an HTTP request;
    System.out.println (JSONSTR);
    Out.print (JSONSTR);
    Jsonobject jsonobj = new Jsonobject (JSONSTR);
    String OpenID = Jsonobj.get ("OpenID"). ToString (); With the user's Opendi can be to the user's information//address for https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid= OPENID&LANG=ZH_CN//Get user information to return to a page model.addattribute ("User", Wechatuser);
  return "Vip/userinfo"; }

The effect is as follows

And this way when users open with other browsers, there will be errors, to ensure that only the use of micro-mail, security. And the address bar will not be exposed to other users ' personal information.

If you have questions, you can view the official website, thank you for your reading, I hope to help you, thank you for your support for this site!

Related Article

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.