Wechat OAuth2.0 Authorization

Introduction

The WeChat Open Platform (微信开放平台) provides a set of APIs that allow you to integrate WeChat login functionality into your website. Users can log in to your site using their WeChat credentials, simplifying the authentication process.

WeChat Open Platform (微信开放平台) Website Application Setup

Within the WeChat Open Platform, create a new website app [创建网页应用].

The WeChat login feature requires a payment of 300 RMB to activate. After actived it, you will see your 微信登录接口状态 [已获得] like below. Take note of AppID and AppSecret credentials that will be used in your website's implementation.

Configure the Authorization Callback Domain [授权回调域]. This should be the domain website where redirect URL at after authorization. The configuration specification for the authorized callback domain requires a full domain name. For example, if the authorized callback domain is www.jinlala.net, after configuration, pages under this domain such as https://www.jinlala.net/wechat-login.php or https://www.jinlala.net/login.html can both undergo OAuth 2.0 authentication.

Implementation on Your Website

login_wechat.php

<?php
$wechatAppID = 'your-wechat-app-id';
$wechatAppSecret = 'your-wechat-app-secret';
$redirectURI = 'your-redirect-url'; //etc: https://www.jinlala.net/login_wechat.php
// Step 1: Generate QR Code URL
$qrCodeURL = "https://open.weixin.qq.com/connect/qrconnect?appid={$wechatAppID}&redirect_uri={$redirectURI}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";


// Step 2: If not authorization (dont have code)
if (!isset($_GET['code'])) {
    // Step 3: redirect user to login QR Code URL and return code
    header("Location: $qrCodeURL");
    exit;

    // Step 4: handle the redirect URI after user authorization
} else {
    // Step 5: Exchange code for access token
    $code = $_GET['code'];
    $accessTokenURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$wechatAppID}&secret={$wechatAppSecret}&code={$code}&grant_type=authorization_code";

    $accessTokenData = json_decode(file_get_contents($accessTokenURL), true);

    if (isset($accessTokenData['access_token'])) {
        // Step 6: Get user information
        $accessToken = $accessTokenData['access_token'];
        $openid = $accessTokenData['openid'];
        $userInfoURL = "https://api.weixin.qq.com/sns/userinfo?access_token={$accessToken}&openid={$openid}";

        $userInfo = json_decode(file_get_contents($userInfoURL), true);
        // Step 6: Process user information
        // $userInfo contains user details (nickname,openid, sex, language, city, province, country, headimgurl, privilege, unionid )
        echo 'username :  ' . $userInfo['nickname'] . '<br>';
        echo 'openID :  ' . $userInfo['openid'] . '<br>';
        echo 'sex :  ' . $userInfo['sex'] . '<br>';
        echo 'language  :  ' . $userInfo['language'] . '<br>';
        echo 'city :  ' . $userInfo['city'] . '<br>';
        echo 'province :  ' . $userInfo['province'] . '<br>';
        echo 'country :  ' . $userInfo['country'] . '<br>';
        echo '<img src="' . $userInfo['headimgurl'] . '" alt = "headimage" ><br>';
        echo 'privilege :  ' . json_encode($userInfo['privilege']) . '<br>';
        echo 'unionid :  ' . $userInfo['unionid'] . '<br>';


    }
}

Last updated