PHPackages                             league/oauth2-github - 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. league/oauth2-github

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

league/oauth2-github
====================

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

3.1.1(1y ago)1122.1M↑16.2%30[2 issues](https://github.com/thephpleague/oauth2-github/issues)[1 PRs](https://github.com/thephpleague/oauth2-github/pulls)20MITPHPPHP ^7.3 || ^8.0

Since Apr 14Pushed 1y ago5 watchersCompare

[ Source](https://github.com/thephpleague/oauth2-github)[ Packagist](https://packagist.org/packages/league/oauth2-github)[ RSS](/packages/league-oauth2-github/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (9)Used By (20)

Github Provider for OAuth 2.0 Client
====================================

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

[![Latest Version](https://camo.githubusercontent.com/50aee0a3ea07e1ac912c340389245a1a5e429a57e14fb408003acf7685894178/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7468657068706c65616775652f6f61757468322d6769746875622e7376673f7374796c653d666c61742d737175617265)](https://github.com/thephpleague/oauth2-github/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/1b7a22c1d023a25a3700aa3eefce8dd0c72bdacfdc00e7cf70adad53695b9f9d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7468657068706c65616775652f6f61757468322d6769746875622f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/thephpleague/oauth2-github)[![Coverage Status](https://camo.githubusercontent.com/adfc971e343235bf8fc29cc7092a4a9f5342e12396888642f3bccec782b2cae8/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f7468657068706c65616775652f6f61757468322d6769746875622e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/thephpleague/oauth2-github/code-structure)[![Quality Score](https://camo.githubusercontent.com/39a7c116baddf3691c3bda780f631eb4745a0824446f8786b3cc53efcc3fa388/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7468657068706c65616775652f6f61757468322d6769746875622e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/thephpleague/oauth2-github)[![Total Downloads](https://camo.githubusercontent.com/f1f58420cd0428c92d52f6586c7a5a21199b4181f93a8b2119570f513d880949/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c65616775652f6f61757468322d6769746875622e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/league/oauth2-github)

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

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

[](#installation)

To install, use composer:

```
composer require league/oauth2-github

```

Usage
-----

[](#usage)

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

### Authorization Code Flow

[](#authorization-code-flow)

```
$provider = new League\OAuth2\Client\Provider\Github([
    'clientId'          => '{github-client-id}',
    'clientSecret'      => '{github-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

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

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

} else {

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

    // Optional: Now you have a token you can look up a users profile data
    try {

        // We got an access token, let's now get the user's details
        $user = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $user->getNickname());

    } catch (Exception $e) {

        // Failed to get user details
        exit('Oh dear...');
    }

    // Use this to interact with an API on the users behalf
    echo $token->getToken();
}
```

### Managing Scopes

[](#managing-scopes)

When creating your Github authorization URL, you can specify the state and scopes your application may authorize.

```
$options = [
    'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
    'scope' => ['user','user:email','repo'] // array or string; at least 'user:email' is required
];

$authorizationUrl = $provider->getAuthorizationUrl($options);
```

If neither are defined, the provider will utilize internal defaults.

At the time of authoring this documentation, the [following scopes are available](https://developer.github.com/v3/oauth/#scopes).

- user
- user:email
- user:follow
- public\_repo
- repo
- repo\_deployment
- repo:status
- delete\_repo
- notifications
- gist
- read:repo\_hook
- write:repo\_hook
- admin:repo\_hook
- admin:org\_hook
- read:org
- write:org
- admin:org
- read:public\_key
- write:public\_key
- admin:public\_key

Testing
-------

[](#testing)

```
$ ./vendor/bin/phpunit
```

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

[](#contributing)

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

Credits
-------

[](#credits)

- [Steven Maguire](https://github.com/stevenmaguire)
- [All Contributors](https://github.com/thephpleague/oauth2-github/contributors)

License
-------

[](#license)

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

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity58

Moderate usage in the ecosystem

Community33

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~710 days

Total

8

Last Release

622d ago

Major Versions

0.2.2 → 2.0.02017-01-26

2.0.0 → 3.0.02021-05-10

PHP version history (3 changes)0.1.0PHP &gt;=5.4.0

0.2.0PHP &gt;=5.5.0

3.0.0PHP ^7.3 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d9c05a30823ae19fc54aa4b4721a696c253f8dec10c7fd22d372cfdc0fcb36d?d=identicon)[stevenmaguire](/maintainers/stevenmaguire)

---

Top Contributors

[![stevenmaguire](https://avatars.githubusercontent.com/u/1851973?v=4)](https://github.com/stevenmaguire "stevenmaguire (16 commits)")[![shadowhand](https://avatars.githubusercontent.com/u/38203?v=4)](https://github.com/shadowhand "shadowhand (11 commits)")[![Gyvastis](https://avatars.githubusercontent.com/u/3257577?v=4)](https://github.com/Gyvastis "Gyvastis (7 commits)")[![fabacino](https://avatars.githubusercontent.com/u/14901115?v=4)](https://github.com/fabacino "fabacino (2 commits)")[![Jefferson49](https://avatars.githubusercontent.com/u/81484983?v=4)](https://github.com/Jefferson49 "Jefferson49 (1 commits)")

---

Tags

clientoauthoauth2authorizationgithubauthorisation

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/league-oauth2-github/health.svg)

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

###  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)[mollie/oauth2-mollie-php

Mollie Provider for OAuth 2.0 Client

251.7M1](/packages/mollie-oauth2-mollie-php)[omines/oauth2-gitlab

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

36721.5k13](/packages/omines-oauth2-gitlab)

PHPackages © 2026

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