{"id":33980970,"url":"https://github.com/samuelthomas2774/oauth-client","last_synced_at":"2025-12-13T03:32:43.476Z","repository":{"id":24241529,"uuid":"27634505","full_name":"samuelthomas2774/oauth-client","owner":"samuelthomas2774","description":"A PHP OAuth 2.0 Client library with built-in support for Facebook, Google, Microsoft, Yahoo, GitHub, LinkedIn and more.","archived":false,"fork":false,"pushed_at":"2019-08-28T17:28:26.000Z","size":328,"stargazers_count":12,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-19T00:27:55.714Z","etag":null,"topics":["oauth","oauth-client","oauth2","oauth2-client","php"],"latest_commit_sha":null,"homepage":"https://samuelthomas2774.github.io/oauth-client/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samuelthomas2774.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-06T13:05:36.000Z","updated_at":"2024-02-01T21:21:36.000Z","dependencies_parsed_at":"2022-08-22T12:10:47.480Z","dependency_job_id":null,"html_url":"https://github.com/samuelthomas2774/oauth-client","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/samuelthomas2774/oauth-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelthomas2774%2Foauth-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelthomas2774%2Foauth-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelthomas2774%2Foauth-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelthomas2774%2Foauth-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samuelthomas2774","download_url":"https://codeload.github.com/samuelthomas2774/oauth-client/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuelthomas2774%2Foauth-client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27699631,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-13T02:00:09.769Z","response_time":147,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["oauth","oauth-client","oauth2","oauth2-client","php"],"created_at":"2025-12-13T03:32:42.821Z","updated_at":"2025-12-13T03:32:43.464Z","avatar_url":"https://github.com/samuelthomas2774.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"OAuth Client\n===\n\nAn OAuth 2.0 Client library with built-in support for Facebook, Google, Microsoft, Yahoo, GitHub, LinkedIn \u0026 more.\n\n\u003e You can see the current source running on Heroku from [examples/all-2](examples/all-2) here:\n\u003e https://oauth-client-test.herokuapp.com\n\n**[Built-in providers](#built-in-providers)**\n\n- [Facebook](https://facebook.com)\n- [Google](https://google.co.uk)\n- [Microsoft](https://microsoft.com/en-gb/)\n- [Yahoo](https://yahoo.co.uk)\n- [GitHub](https://github.com)\n- [LinkedIn](https://linkedin.com)\n- [Spotify](https://spotify.com)\n- [Amazon](https://amazon.co.uk)\n- [Disqus](https://disqus.com)\n- [Instagram](https://instagram.com)\n- [TeamViewer](https://teamviewer.com)\n- [WordPress.com](https://wordpress.com)\n- [Eventbrite](https://eventbrite.com)\n- [Foursquare](https://foursquare.com)\n- [SpeechMore](https://speechmore.ml)\n- [GitLab](https://about.gitlab.com)\n- [Mastodon](https://joinmastodon.org)\n- [Discord](https://discordapp.com)\n- [Pinterest](https://pinterest.co.uk)\n- [Slack](https://slack.com)\n- [DigitalOcean](https://digitalocean.com)\n- [Gitter](https://gitter.im)\n- [Deezer](https://deezer.com)\n- [DeviantArt](https://deviantart.com)\n- [Twitch](https://twitch.tv)\n- [Vimeo](https://vimeo.com)\n- [Reddit](https://reddit.com)\n- Other, just create a new `OAuth2\\\\GenericOAuthProvider` object and include the `base_api_endpoint`,\n    `authorise_endpoint` and `token_endpoint` options\n\nRequirements and installation\n---\n\nThis requires at least PHP 7.1.\n\nVersion | Supported\n--------|----------\nPHP 5.6 | No\nPHP 7.0 | No\nPHP 7.1 | Yes\nPHP 7.2 | Yes\nPHP 7.3 | Yes\n\n### Composer\n\n1. Add `\"samuelthomas2774/oauth-client\": \"~3.0.0\"` to your composer.json\n    ```json\n    {\n        \"require\": {\n            \"samuelthomas2774/oauth-client\": \"~3.0.0\"\n        }\n    }\n    ```\n2. Run Composer  \n    This will automatically download the latest patch version.\n    ```\n    composer install\n    ```\n\nUsage\n---\n\n1. Include `vendor/autoload.php` in all pages that need access to any provider  \n    This will load any class in the src directory when used.\n    ```php\n    require_once __DIR__ . '/vendor/autoload.php';\n    ```\n2. Create a new `OAuth2\\\\OAuth` object with the parameters `$client_id`, `$client_secret` and `$options`  \n    The `$options` array must have at least `base_api_endpoint`, `authorise_endpoint` and `token_endpoint`.\n    ```php\n    use OAuth2\\GenericOAuthProvider;\n\n    $client_id = 'client-id';\n    $client_secret = 'client-secret';\n\n    $client = new GenericOAuthProvider($client_id, $client_secret, null, [\n        'session_prefix' =\u003e 'facebook_',\n        'base_api_endpoint' =\u003e 'https://graph.facebook.com',\n        'authorise_endpoint' =\u003e 'https://facebook.com/dialog/oauth',\n        'token_endpoint' =\u003e 'oauth/access_token', // Relative to the base API URL\n    ]);\n    ```\n\n\n- To get a link to the authorise page:\n    ```php\n    $redirect_url = 'https://example.com/facebook/code.php';\n    $scope = ['email', 'user_friends']; // Optional scope array\n\n    // Returns an OAuth2\\AuthoriseUrl object\n    // This will be converted to a string automatically if needed\n    // You can use this object to get the OAuth2\\State object to store extra state data accessible with\n    // $client-\u003egetRequestState() when the user accepts/rejects the authorise request\n    $login_url = $client-\u003egenerateAuthoriseUrlAndState($redirect_url, $scope);\n\n    // Add data to the state object\n    // Every time generateAuthoriseUrlAndState is called a new state object will be created with separate data,\n    // so you can have multiple authorise links on the same page and have the authorise response page react differently\n    // depending on which link was used\n    $login_url-\u003egetState()-\u003enext_url = '/';\n    ```\n    ```php\n    // You can also set the state parameter yourself (not recommended)\n    // $login_url-\u003egetState() will still return a state object, but the attached data won't be saved\n    // Use $login_url-\u003egetStateId(), $login_url-\u003egetState()-\u003egetId() or cast the state object to a string to get the\n    // value of the state parameter\n    $state = '...'; // Generate a random value and store in somewhere the next page can access (or set to null to not set a state parameter)\n    $login_url = $client-\u003egenerateAuthoriseUrl($state, $redirect_url, $scope);\n    ```\n- To get an access token from the code that was returned:\n    ```php\n    // Validates the request state and returns an OAuth2\\AccessToken object\n    $token = $client-\u003egetAccessTokenFromRequestCodeAndState();\n    ```\n    ```php\n    // You can also validate the state and pass the redirect url and an optional requested scope array yourself\n    // Only do this if you set the state parameter when you generated the authorise link\n\n    $redirect_url = 'https://example.com/facebook/code.php'; // Must match the $redirect_url given to generateAuthoriseUrl exactly\n    $requested_scope = ['email', 'user_friends']; // Optional requested scope array to include in the AccessToken object\n    $token = $client-\u003egetAccessTokenFromRequestCode($redirect_url, $requested_scope);\n    ```\n- To get an access token a refresh token:\n    ```php\n    // Returns an object of data returned from the server\n    // This may include a new refresh_token\n    // $token can be a string (a refresh token) or an AccessToken object with a refresh token returned by all\n    // getAccessToken* methods\n    $new_token = $oauth-\u003egetAccessTokenFromRefreshToken($token);\n    ```\n- To make API requests with the access token:\n    ```php\n    try {\n        $response = $client-\u003eapi('GET', 'me' /*, $guzzle_options = [] /*, $auth = null */);\n    } catch (Exception $error) {\n        echo 'OAuth provider returned an error: ' . print_r($error, true);\n    }\n    ```\n- To get/set the current access token:\n    You do not need to do this at the start of the script to get the access token from the session, this is done\n    automatically. Also, this function updates the access token in the session.\n    ```php\n    // Get\n    $token = $client-\u003egetAccessToken();\n\n    // Set\n    $client-\u003esetAccessToken($new_token);\n\n    // Set without updating the access token in the session\n    $client-\u003esetAccessToken($new_token, false);\n    ```\n\nBuilt-in providers\n---\n\nAny other providers please contact me at https://samuelelliott.ml/#contact and I'll add it as soon as possible.\n\n**Provider**        | **Class**                                     | **Sign-up url**\n--------------------|-----------------------------------------------|-------------------------\nAmazon              | OAuth2\\\\Providers\\\\Amazon\\\\Amazon             | https://developer.amazon.com/lwa/sp/overview.html\nDeezer              | OAuth2\\\\Providers\\\\Deezer\\\\Deezer             | https://developers.deezer.com/myapps/create\nDeviantArt          | OAuth2\\\\Providers\\\\DeviantArt\\\\DeviantArt     | https://www.deviantart.com/developers/register\nDigitalOcean        | OAuth2\\\\Providers\\\\DigitalOcean\\\\DigitalOcean | https://cloud.digitalocean.com/account/api/applications/new\nDiscord `P`         | OAuth2\\\\Providers\\\\Discord\\\\Discord           | https://discordapp.com/developers/applications/\nDisqus              | OAuth2\\\\Providers\\\\Disqus\\\\Disqus             | https://disqus.com/api/applications/register/\nEventbrite          | OAuth2\\\\Providers\\\\Eventbrite\\\\Eventbrite     | https://www.eventbrite.co.uk/myaccount/apps/new/\nFacebook `P`        | OAuth2\\\\Providers\\\\Facebook\\\\Facebook         | https://developers.facebook.com/apps/\nFoursquare          | OAuth2\\\\Providers\\\\Foursquare\\\\Foursquare     | https://foursquare.com/developers/apps\nGitHub `P`          | OAuth2\\\\Providers\\\\GitHub\\\\GitHub             | https://github.com/settings/applications/new\nGitLab `P` `I`      | OAuth2\\\\Providers\\\\GitLab\\\\GitLab             | https://gitlab.com/profile/applications *\nGitter              | OAuth2\\\\Providers\\\\Gitter\\\\Gitter             | https://developer.gitter.im/apps/new\nGoogle `P`          | OAuth2\\\\Providers\\\\Google\\\\Google             | https://console.developers.google.com\nInstagram           | OAuth2\\\\Providers\\\\Instagram\\\\Instagram       | https://instagram.com/developer/clients/register/\nLinkedIn `P`        | OAuth2\\\\Providers\\\\Linkedin\\\\Linkedin         | https://www.linkedin.com/developer/apps/new\nMastodon `P` `I`    | OAuth2\\\\Providers\\\\Mastodon\\\\Mastodon         | https://mastodon.social/settings/applications/new *\nMicrosoft           | OAuth2\\\\Providers\\\\Microsoft\\\\Microsoft       | https://account.live.com/developers/applications/create\nPinterest           | OAuth2\\\\Providers\\\\Pinterest\\\\Pinterest       | https://developers.pinterest.com/apps/\nReddit              | OAuth2\\\\Providers\\\\Reddit\\\\Reddit             | https://www.reddit.com/prefs/apps\nSlack `P`           | OAuth2\\\\Providers\\\\Slack\\\\Slack               | https://api.slack.com/apps\nSpeechMore `P`      | OAuth2\\\\Providers\\\\SpeechMore\\\\SpeechMore     | https://speechmore.ml/settings/api-clients\nSpotify             | OAuth2\\\\Providers\\\\Spotify\\\\Spotify           | https://developer.spotify.com/my-applications/#!/applications/create\nTeamViewer          | OAuth2\\\\Providers\\\\TeamViewer\\\\TeamViewer     | https://login.teamviewer.com/nav/api\nTwitch              | OAuth2\\\\Providers\\\\Twitch\\\\Twitch             | https://dev.twitch.tv/dashboard/apps/create\nVimeo               | OAuth2\\\\Providers\\\\Vimeo\\\\Vimeo               | https://developer.vimeo.com/apps/new\nWordPress.com       | OAuth2\\\\Providers\\\\WordPress\\\\WordPress       | https://developer.wordpress.com/apps/new/\nYahoo `P`           | OAuth2\\\\Providers\\\\Yahoo\\\\Yahoo               | https://developer.apps.yahoo.com/dashboard/createKey.html\n\n\\* This URL is for the default instance used\n\nAll the built-in providers implement `OAuth2\\UserProfilesInterface`, which adds an extra method to get a\n`OAuth2\\UserProfile` object.\n\n```php\ntry {\n    $user = $client-\u003egetUserProfile();\n} catch (Exception $error) {\n    echo 'OAuth provider returned an error: ' . print_r($error, true);\n}\n```\n\nSome also implement `OAuth2\\UserPicturesInterface` (`P`), which adds a method to get the URL of the user's\npicture/avatar.\n\n```php\ntry {\n    $picture_url = $client-\u003egetUserPictureUrl();\n} catch (Exception $error) {\n    echo 'OAuth provider returned an error: ' . print_r($error, true);\n}\n```\n\nAs GitLab and Mastodon support multiple instances they implement `OAuth2\\MultipleInstancesInterface`, which adds an\noption and method to set the instance's URL.\n\n```php\nuse OAuth2\\Providers\\GitLab\\GitLab;\n\n$client = new GitLab($client_id, $client_secret, null, [\n    'instance_url' =\u003e 'https://gitlab.fancy.org.uk',\n]);\n\n$client-\u003esetInstanceUrl('https://gitlab.com');\n```\n\n### Additional methods for providers\n\n[Facebook](src/Providers/Facebook/README.md) and [Discord](src/Providers/Discord/README.md) have additional methods.\n\nExtending the OAuth2 class\n---\n\nYou can extend the OAuth2 and other classes to add new functions and make existing functions work differently:\n\n```php\nuse OAuth2\\Providers\\Facebook\\Facebook;\n\nclass My_Extended_Facebook_Class extends Facebook\n{\n    // Change the base_api_endpoint option to use a different API version\n    public $base_api_endpoint = 'https://graph.facebook.com/v1.0/';\n\n    // Add a new function for getting the current user's ID\n    public function getUserId(): string\n    {\n        $user = $this-\u003egetUserProfile(['id']);\n        return $user-\u003eid;\n    }\n}\n```\n\nYou can then use the newly created class:\n\n```php\n$client = new My_Extended_Facebook_Class('client-id', 'client-secret');\n\ntry {\n    echo 'Your Facebook User ID (for this app) is: ' . htmlentities($client-\u003egetUserId());\n} catch (Exception $exception) {\n    echo 'Facebook returned an error: ' . $exception-\u003egetMessage();\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuelthomas2774%2Foauth-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamuelthomas2774%2Foauth-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuelthomas2774%2Foauth-client/lists"}