search
HomeWeChat AppletWeChat DevelopmentWeChat development - obtaining user's basic information through authorization

1. Fill in the domain name of the authorization callback page

Fill in wx.alinq.org here. After obtaining the user's authorization, it will jump to a page specified by the developer, and the link to the page must be under the domain name. If not filled in, a page with invalid page links will appear.

WeChat development - obtaining users basic information through authorization

WeChat development - obtaining users basic information through authorization

2. Guide the user to the specified authorization page

For example: https://open.weixin.qq .com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

For the explanation of parameters, please refer to WeChat related documents. It is worth noting that redirect_uri is a url, which must be url encoded. After generating the url, you can go to http://cli.im/text/2014052714?4QbdC to generate a QR code for the url, and then Scan it in WeChat to test it.

WeChat development - obtaining users basic information through authorization

After scanning:

WeChat development - obtaining users basic information through authorization

3. Finally, here is the complete implementation code

Below is the complete code, I hope it is useful to everyone. ^_^

<%@ WebHandler Language="C#" %>

public class UserAuth : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
 
        var appid = "wxf1c24c60e3ac13b7";
        var secret = "5902b9817acb7a290d4b7c2e6e97d4d3";

        var code = context.Request.QueryString["Code"];
        if (string.IsNullOrEmpty(code))
        {
            var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http%3a%2f%2fwx.alinq.org%2fTest%2fUserAuth.ashx&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect", appid);
            context.Response.Redirect(url);
        }
        else
        {
            var client = new System.Net.WebClient();
            client.Encoding = System.Text.Encoding.UTF8;

            var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code);
            var data = client.DownloadString(url);

            var serializer = new JavaScriptSerializer();
            var obj = serializer.Deserialize<Dictionary<string, string>>(data);
            string accessToken;
            if (!obj.TryGetValue("access_token", out accessToken))
                return;

            var opentid = obj["openid"];
            url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken, opentid);
            data = client.DownloadString(url);
            var userInfo = serializer.Deserialize<Dictionary<string, object>>(data);
            foreach (var key in userInfo.Keys)
            {
                context.Response.Write(string.Format("{0}: {1}", key, userInfo[key]) + "<br/>");
            }
        }
    }
}

For more WeChat development - obtaining user’s basic information through authorization, please pay attention to the PHP Chinese website for related articles!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)