Interact with the Patreon API via OAuth

0.3.1 2018-05-18 20:57 UTC


Interact with the Patreon API via OAuth.


Get the plugin from Packagist


Step 1. Get your client_id and client_secret

Visit the Patreon platform documentation page while logged in as a Patreon creator to register your client.

This will provide you with a client_id and a client_secret.

Step 2. Use this plugin in your code

Let's say you wanted to make a "Log In with Patreon" button. You've read through the directions, and are trying to implement "Step 2: Handling the OAuth Redirect" with your server. The user will be arriving at one of your pages after you have sent them to the authorize page (at for step 1, so in their query parameters landing on this page, they will have a parameter 'code'.

(If you are doing something other than the "Log In with Patreon" flow, please see the examples folder for more examples)


 * Use the following,
 * or if you installed via Composer, it should already be required via autoloader

use Patreon\API;
use Patreon\OAuth;

$client_id = null;      // Replace with your data
$client_secret = null;  // Replace with your data

$oauth_client = new OAuth($client_id, $client_secret);

// Replace http://localhost:5000/oauth/redirect with your own uri
$redirect_uri = "http://localhost:5000/oauth/redirect";
 * Make sure that you're using this snippet as Step 2 of the OAuth flow:
 * so that you have the 'code' query parameter.
$tokens = $oauth_client->get_tokens($_GET['code'], $redirect_uri);
$access_token = $tokens['access_token'];
$refresh_token = $tokens['refresh_token'];

$api_client = new API($access_token);
$patron_response = $api_client->fetch_user();
 * The $patron_response is now a [art4/json-api-client/Document](
 * You can access its primary data via ->get('data')
$patron = $patron_response->get('data');
// Can now check attributes, e.g. $patron->attribute('full_name');
$pledge = null;
 * Given a resource in the document, you can access its relationships with ->relationship->($relationship_name)
 * or check for a relationship's existence with ->has('relationships.relationship_name')
if ($patron->has('relationships.pledges')) {
     * To look up the full resource that the relationship is referencing,
     * we have extended art4/json-api-client/ResourceIdentifier
     * (
     * with the `->resolve` method.
     * You pass in the original response document, and you get back a full resource,
     * with attributes, relationships, etc.
    $pledge = $patron->relationship('pledges')->get(0)->resolve($patron_response);
    // Can now check attributes, e.g. $pledge->attribute('amount_cents');

 $patron will have the authenticated user's user data, and
 $pledge will have their patronage data.
 Typically, you will save the relevant pieces of this data to your database,
 linked with their user account on your site,
 so your site can customize its experience based on their Patreon data.
 You will also want to save their $access_token and $refresh_token to your database,
 linked to their user account on your site,
 so that you can refresh their Patreon data on your own schedule.