PHPackages                             oakhope/oauth2-wechat - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. oakhope/oauth2-wechat

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

oakhope/oauth2-wechat
=====================

微信登录认证授权 Wechat login authorization. This package provides Wechat OAuth 2.0 support for the PHP League's OAuth 2.0 Client

v1.0.4(8y ago)228.9k↓50%6MITPHP

Since Sep 8Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/oakhope/oauth2-wechat)[ Packagist](https://packagist.org/packages/oakhope/oauth2-wechat)[ Docs](https://github.com/oakhope/oauth2-wechat)[ RSS](/packages/oakhope-oauth2-wechat/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Wechat Provider for OAuth 2.0 Client
====================================

[](#wechat-provider-for-oauth-20-client)

[![Latest Version](https://camo.githubusercontent.com/5c2786bd4fe61653ff6e7b36802d216b5dff8dc705ea4cb5574cbcae4de76ae6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6f616b686f70652f6f61757468322d7765636861742e7376673f7374796c653d666c61742d737175617265)](https://github.com/oakhope/oauth2-wechat/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://camo.githubusercontent.com/3732f69d40fd4a1fb67113cb7275f6ef3172a294a1efd975cb4369855b16f8ef/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6f616b686f70652f6f61757468322d7765636861742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/oakhope/oauth2-wechat)[![Coverage Status](https://camo.githubusercontent.com/3eaecce93578f319b6b8cac73fcc225a75b24ce93c39d562d63bd97b10a9d342/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6f616b686f70652f6f61757468322d7765636861742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/oakhope/oauth2-wechat/code-structure)[![Quality Score](https://camo.githubusercontent.com/353cbe9af32e32ac41272b52580922d8290c7932ba1ae9e735f655253d629bcc/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6f616b686f70652f6f61757468322d7765636861742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/oakhope/oauth2-wechat)[![Total Downloads](https://camo.githubusercontent.com/7cfb4c19217285ded9cc0040bc5fff8565874c9701caa588ba9c53119585814c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f616b686f70652f6f61757468322d7765636861742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oakhope/oauth2-wechat)

This package provides Wechat OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).

- DONE:

    > Website SDK, Mini Programs
- TODO:

    > Mobile App SDK

Installation
------------

[](#installation)

To install, use composer:

```
composer require oakhope/oauth2-wechat

```

Usage
-----

[](#usage)

Usage is the same as The League's OAuth client, using `\Oakhope\OAuth2\Client\Provider\{WebProvider}` as the provider.

### Authorization Code Flow

[](#authorization-code-flow)

```
$provider = new \Oakhope\OAuth2\Client\Provider\WebProvider([
        'appid' => '{wechat-client-id}',
        'secret' => '{wechat-client-secret}',
        'redirect_uri' => 'https://example.com/callback-url'
    ]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl();

    // Get the state generated for you and store it to the session.
    $_SESSION['oauth2state'] = $provider->getState();

    // Redirect the user to the authorization URL.
    header('Location: '.$authorizationUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== rtrim($_SESSION['oauth2state'], '#wechat_redirect'))) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    try {

        // Try to get an access token using the authorization code grant.
        $accessToken = $provider->getAccessToken(
            'authorization_code',
            [
                'code' => $_GET['code'],
            ]);

        // We have an access token, which we may use in authenticated
        // requests against the service provider's API.
        echo "token: ".$accessToken->getToken()."";
        echo "refreshToken: ".$accessToken->getRefreshToken()."";
        echo "Expires: ".$accessToken->getExpires()."";
        echo ($accessToken->hasExpired() ? 'expired' : 'not expired')."";

        // Using the access token, we may look up details about the
        // resource owner.
        $resourceOwner = $provider->getResourceOwner($accessToken);

        var_export($resourceOwner->toArray());

    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

        // Failed to get the access token or user details.
        echo "error:";
        exit($e->getMessage());
    }
}
```

### Refreshing a Token

[](#refreshing-a-token)

Once your application is authorized, you can refresh an expired token using a refresh token rather than going through the entire process of obtaining a brand new token. To do so, simply reuse this refresh token from your data store to request a refresh.

*This example uses [Brent Shaffer's](https://github.com/bshaffer) demo OAuth 2.0 application named **Lock'd In**. See authorization code example above, for more details.*

```
$provider = new \Oakhope\OAuth2\Client\Provider\WebProvider([
        'appid' => '{wechat-client-id}',
        'secret' => '{wechat-client-secret}',
        'redirect_uri' => 'https://example.com/callback-url'
    ]);

$existingAccessToken = getAccessTokenFromYourDataStore();

if ($existingAccessToken->hasExpired()) {
    $newAccessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $existingAccessToken->getRefreshToken()
    ]);

    // Purge old access token and store new access token to your data store.
}
```

Testing
-------

[](#testing)

```
$ ./vendor/bin/phpunit --colors tests
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/oakhope/oauth2-wechat/blob/master/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Benji Wang](https://github.com/oakhope)
- [All Contributors](https://github.com/oakhope/oauth2-wechat/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/oakhope/oauth2-wechat/blob/master/LICENSE) for more information.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance54

Moderate activity, may be stable

Popularity33

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

2

Last Release

3168d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/da6c8b689348fd0ccbeaa738f9ac6b9d5f514e13dca1eff9831e33cf13577117?d=identicon)[oakhope](/maintainers/oakhope)

---

Top Contributors

[![oakhope](https://avatars.githubusercontent.com/u/9282209?v=4)](https://github.com/oakhope "oakhope (57 commits)")

---

Tags

authorisationauthorizationclientloginoauthoauth2wechatwexinclientoauthoauth2authorizationauthorisationwechatweixinweixin loginwechat loginoauth2 wechatoauth2 weixin

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/oakhope-oauth2-wechat/health.svg)

```
[![Health](https://phpackages.com/badges/oakhope-oauth2-wechat/health.svg)](https://phpackages.com/packages/oakhope-oauth2-wechat)
```

###  Alternatives

[stevenmaguire/oauth2-keycloak

Keycloak OAuth 2.0 Client Provider for The PHP League OAuth2-Client

2275.9M27](/packages/stevenmaguire-oauth2-keycloak)[patrickbussmann/oauth2-apple

Sign in with Apple OAuth 2.0 Client Provider for The PHP League OAuth2-Client

1132.5M6](/packages/patrickbussmann-oauth2-apple)[league/oauth2-instagram

Instagram OAuth 2.0 Client Provider for The PHP League OAuth2-Client

761.0M31](/packages/league-oauth2-instagram)[stevenmaguire/oauth2-salesforce

Salesforce OAuth 2.0 Client Provider for The PHP League OAuth2-Client

311.6M3](/packages/stevenmaguire-oauth2-salesforce)[mollie/oauth2-mollie-php

Mollie Provider for OAuth 2.0 Client

251.7M1](/packages/mollie-oauth2-mollie-php)[dalpras/oauth2-gotowebinar

LogMeIn GoToWebinar OAuth 2.0 Client Provider for the PHP League's OAuth 2.0 Client

1244.3k](/packages/dalpras-oauth2-gotowebinar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
