PHPackages                             fmwww/passport-wechat-oauth-grant - 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. fmwww/passport-wechat-oauth-grant

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

fmwww/passport-wechat-oauth-grant
=================================

Wechat Oauth Grant For Passport

v1.0.4(8y ago)1158PHP

Since Mar 25Pushed 8y ago1 watchersCompare

[ Source](https://github.com/fmwww/passport-wechat-oauth-grant)[ Packagist](https://packagist.org/packages/fmwww/passport-wechat-oauth-grant)[ RSS](/packages/fmwww-passport-wechat-oauth-grant/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (2)Versions (6)Used By (0)

passport-wechat-oauth-grant
===========================

[](#passport-wechat-oauth-grant)

增加一个可以通过微信Oauth授权code登录passport的Grant

安装
--

[](#安装)

通过Composer安装

```
$ composer require fmwww/passport-wechat-oauth-grant
```

如果你的Laravel版本 &lt; 5.5, 你需要在 `config/app.php` 的providers数组中增加:

```
Fmwww\PassportWechatOauthGrant\WechatOauthGrantServiceProvider::class,
```

配置
--

[](#配置)

使用之前需要先配置好微信的 `app_id` 和 `app_secret`使用下面的命令拷贝配置文件到 `config` :

```
$ php artisan vendor:publish --provider="Fmwww\PassportWechatOauthGrant\WechatOauthGrantServiceProvider"
```

```
return [
    /*
     * 微信app_id
     */
    'app_id' => env('WECHAT_OAUTH_GRANT_APP_ID', ''),
    /*
     * 微信app_secret
     */
    'app_secret' => env('WECHAT_OAUTH_GRANT_APP_SECRET', ''),
];
```

> 推荐使用 `.env` 文件进行配置

用法
--

[](#用法)

- 使用**POST**方法去请求`https://your-site.com/oauth/token`
- **POST**请求体里的需要将**grant\_type**设置为**wechat\_oauth**，同时将**code**设置为**微信返回的授权code**
- 系统将会根据 `config/auth.php`里面的 `api guard` 设置的用户模型去寻找用户，如果用户模型定义了 `findForWechatOauth` 方法，那么就会使用这个方法返回的用户进行认证，否则就会根据`openid`字段去寻找用户进行认证。
- 如果用户存在，就会成功返回 `access_token` 和 `refresh_token`。

### 例子

[](#例子)

#### 请求方法

[](#请求方法)

```
$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'wechat_oauth',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'code' => '001qJKS42a3r3N0wrDU42IHrS42qJKSN', #微信的授权code
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);
```

#### 自定义 `findForWechatOauth`方法

[](#自定义-findforwechatoauth方法)

```
public function findForWechatOauth($tokens)
    {
        // 获取openid
        $openid = $tokens->openid;

        // 获取access_token
        $access_token = $tokens->access_token;

        // 获取expires_in
        $expires_in = $tokens->expires_in;

        // 获取refresh_token
        $refresh_token = $tokens->refresh_token;

        // 获取scope
        $scope = $tokens->scope;

        // 通过openid查找，如果这个方法没有定义，默认就是这样查找
        $user = $this->where('openid', $openid)->first();

        // 你也可以自己创建用户然后返回
        if (empty($user)) {
            $this->openid = $openid;
            $this->save();
        }
        // 返回用户
        return $user ?: $this;
    }
```

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity67

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 ~2 days

Total

5

Last Release

3010d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/11371523?v=4)[FMW](/maintainers/fmwww)[@fmwww](https://github.com/fmwww)

---

Top Contributors

[![fmwww](https://avatars.githubusercontent.com/u/11371523?v=4)](https://github.com/fmwww "fmwww (10 commits)")

---

Tags

grantpassportwechat-oauth

### Embed Badge

![Health badge](/badges/fmwww-passport-wechat-oauth-grant/health.svg)

```
[![Health](https://phpackages.com/badges/fmwww-passport-wechat-oauth-grant/health.svg)](https://phpackages.com/packages/fmwww-passport-wechat-oauth-grant)
```

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.2k532.1M2.5k](/packages/aws-aws-sdk-php)[unopim/unopim

UnoPim Laravel PIM

10.5k2.2k](/packages/unopim-unopim)[overtrue/socialite

A collection of OAuth 2 packages.

1.4k5.6M90](/packages/overtrue-socialite)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k496.1k33](/packages/neuron-core-neuron-ai)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3661.2M46](/packages/tencentcloud-tencentcloud-sdk-php)[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k12](/packages/tempest-framework)

PHPackages © 2026

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