View on GitHub

OAuth 2.0 Client

A PHP OAuth 2.0 Client library with built-in support for Facebook, Google, Microsoft, Yahoo, GitHub & Linkedin and more.

Download this project as a .zip file Download this project as a tar.gz file

Built-in providers

Default (OAuth2)

  1. Include src/oauth.class.php in all page that need access to your other provider.

    require_once __DIR__ . '/oauth-client/src/oauth.class.php';
  2. Create a new OAuth2 object with the parameters $client_id, $client_secret and $options. The $options array must have at least dialog->base_url, api->base_url and requests->{"/oauth/token"}.

    $client_id = "appid";
    $client_secret = "appsecret";
    $oauth = new OAuth2(
        $client_id, $client_secret,
        $options = Array(
            "dialog" => Array("base_url" => ""),
            "api" => Array("base_url" => ""),
            "requests" => Array("/oauth/token" => "/oauth/access_token")

You can also use these methods in extended classes (subclasses).

Built-in providers

Any other providers please contact me at and I'll add it as soon as possible.

Provider Class File in /src Sign-up url
Facebook U P OAuthFacebook facebook.class.php
Google U P OAuthGoogle google.class.php
Microsoft U P OAuthMicrosoft microsoft.class.php
Yahoo U P OAuthYahoo yahoo.class.php
GitHub U P OAuthGitHub github.class.php
LinkedIn U P OAuthLinkedin linkedin.class.php
Amazon U OAuthAmazon amazon.class.php
Disqus U OAuthDisqus disqus.class.php
Instagram U OAuthInstagram instagram.class.php
Spotify U OAuthSpotify spotify.class.php!/applications/create U P OAuthST st.class.php
TeamViewer U OAuthTeamViewer teamviewer.class.php U OAuthWordPress wordpress.class.php

All the built-in providers above have an extra method, userProfile U, that returns the user's data in an object:

try { $user = $oauth->userProfile(); }
catch(Exception $error) { exit("OAuth Provider returned an error: " . print_r($error, true)); }

// $user == stdClass::__set_state(Array("id" => 1, "username" => "samuelthomas2774", "name" => "Samuel Elliott", "email" => null, "response" => $response_from_server));

Some also have a profilePicture method P, that returns the user's profile picture and an <img /> tag:

try { $picture = $oauth->userProfile(); }
catch(Exception $error) { exit("OAuth Provider returned an error: " . print_r($error, true)); }

// $picture == stdClass::__set_state(Array("url" => "", "tag" => "<img src=\"\" style=\"width:50px;height:50px;\" />"));


The OAuthFacebook class has some additional methods:

Extending the OAuth2 class.

You can extend the OAuth2 and other classes to add new functions and make existing functions work differently:

require_once __DIR__ . '/oauth-client/src/facebook.class.php';
class My_Extended_Facebook_Class extends OAuthFacebook {
    // Options. Customize default options (optional).
    protected $options = Array(
        // Change the api->base_url option to use a different api version.
        "api" => Array("base_url" => "")

    // Add a new function for getting the current user's id.
    public function userid() {
        $user = $this->userProfile(Array("id"));
        return $user->id;

You can then use the newly created class:

$oauth = new My_Extended_Facebook_Class("appid", "appsecret");
try { echo "Your Facebook User ID (for this app) is: " . $oauth->userid(); }
catch(Exception $error) { exit("Facebook returned an error: " . print_r($error, true)); }