Skip to content
This repository has been archived by the owner on Sep 26, 2020. It is now read-only.

Latest commit

 

History

History
58 lines (45 loc) · 1.77 KB

02-oauth.md

File metadata and controls

58 lines (45 loc) · 1.77 KB
  1. Getting Started
  2. Obtaining OAuth Tokens For Patrons
  3. Patreon Integration Examples
  4. Method Documentation
  5. Library Architecture

Logging In With OAuth

Patreon implements the standard OAuth 2.0 login flow which is compatible with any standards compliant OAuth 2.0 Client, including The League of Extraordinary Packages OAuth 2.0 Client and Laravel Socialite.

OAuth 2.0 Patreon PHP includes a simple Patreon OAuth Client which you can use to accept user logins.

  1. A user visits your login page
  2. You redirect the user to the Patreon login page
  3. The user is asked the user to confirm that they authorise your application
  4. Patreon redirects the user back to your redirect_uri with a code query parameter
  5. You make a request to Patreon exchanging the code for an access_token and refresh_token

After you have obtained the access token you are able to communicate with the Patreon API on behalf of the user. You should store the user's access token and refresh token in your database.

require_once 'vendor/autoload.php';

use Squid\Patreon\OAuth;
use Squid\Patreon\Patreon;

$oauth = new OAuth(
    'client_id',
    'client_secret',
    'redirect_uri'
);

if (! isset($_GET['code'])) {
    Header('Location: ' . $oauth2->getAuthorizationUrl());
    exit;
}

$tokens = $oauth->getAccessToken($_GET['code']);

$patreon = new Patreon($tokens['access_token']);

$user = $patreon->me()->get();

echo "Hello, {$user->full_name}!";
if ($user->hasActivePledge()) {
    echo "You are a patron of my campaign, thank you!";
} else {
    echo "You are not a patron of my campaign.";
}