Lost beginner ;-)

Reviewing the code in patron-list.php we can see that the error you’re encountering happens when the access token you’ve provided is not valid: the example code then tries to get a new access token using the refresh token, which is triggering the error you’re seeing, because the refresh token is not valid either. You can rectify this issue by verifying your tokens are correct, I made a post previously that gives a way to check a token is valid or not via the command line.

There’s 2 options here: we can try and put together what you’re trying to achieve using the official patreon-php library, or if you’re using PHP 7.2 you could try my unofficial library called Patreon PHP instead which includes an example project with a step by step guide that you can use for your use case, as it is more suited to implementing projects like this.

You can obtain a copy of the Patreon PHP Example project and take a look at how things are implemented which should give you a good idea of what functionality is available and how you can use it. For the sake of making things easy though, I’ll include a step by step guide below on how to implement patron level page gating using my unofficial Patreon PHP library.


There’s a few different ways you could implement this, the easiest way I think would be something like this:

  1. Create an index page that lists all of the patron-limited pages on your website (e.g: download, settings) and the amount required to access them
  2. On each page (e.g: download, settings) check if the patron pledges enough to have access, if not, redirect them back to the index

The method for checking eligibility would be:

  1. Fetch the user’s information from Patreon
  2. Determine if the user has an active pledge
  3. Check if their pledge meets the eligibility criteria

You could determine download eligibility based on either dollar amounts or reward ID. Dollar amounts would be easier if you wish to have cascading eligibility (e.g: $1 is eligible for a thing, $10 is eligible for the $1 thing and a $10 thing) but you could do it with IDs that way too if you store a map of ID to page eligibility. For the sake of making it an easy example I’ll limit pages based on dollar amounts.

You’ll need the following pages:

  1. An index page to display a list of other pages
  2. A login page to accept logins and create a session
  3. Your pages that are limited to patrons

First things first create a new project directory, and then install the Patreon PHP library using composer:

composer require squid/patreon php-http/guzzle6-adapter

Now create a file called bootstrap.php, this file will be included at the start of every page as it sets the configuration values and starts the session. You can obtain the Patreon configuration values via Clients & API Keys.

<?php require_once __DIR__ . '/vendor/autoload.php';

session_start();

define('PATREON_CLIENT_ID', 'your-client-id-goes-here');
define('PATREON_CLIENT_SECRET', 'your-client-secret-goes-here');
define('PATREON_REDIRECT_URI', 'http://example.com/login.php');

Then create a new directory called public, this is where the pages that people should be able to access are placed, the first of which is the login.php page. This is the page that handles the Patreon login with OAuth.

<?php require_once __DIR__ . '/../bootstrap.php';

use Squid\Patreon\Exceptions\OAuthReturnedError;
use Squid\Patreon\OAuth;

$oauth = new OAuth(PATREON_CLIENT_ID, PATREON_CLIENT_SECRET, PATREON_REDIRECT_URI);

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

try {
    $tokens = $oauth->getAccessToken($_GET['code']);
} catch(OAuthReturnedError $e) {
    echo "An error occurred completing your login: {$e->getMessage()}";
    exit;
}

$_SESSION['accessToken'] = $tokens['access_token'];

Header('Location: index.php');
exit;

After you’ve created the login page you’ll need to make sure to add the login page URL to your Patreon clients approved redirect URIs.

The next page is the index page, create index.php in the public directory:

<?php require_once __DIR__ . '/../bootstrap.php';

if (! isset($_SESSION['accessToken'])) {
    Header('Location: login.php');
    exit;
}

use Squid\Patreon\Patreon;

$patreon = new Patreon($_SESSION['accessToken']);
$patron = $patreon->me()->get();

$pages = [
    'manual' => [
        'minimum' => 100,
        'url' => 'manual.php',
        'title' => 'Manual',
        'description' => 'Download the manual in PDF format',
    ],
    'settings' => [
        'minimum' => 1000,
        'url' => 'settings.php',
        'title' => 'Settings',
        'description' => 'Download setting files and upload setting files for analyzes',
    ],
    'example' => [
        'minimum' => 2324324,
        'url' => 'example.php',
        'title' => 'Example',
        'description' => 'An example...'
    ]
];

?>
<html>
  <body>
    <ul>
      <?php foreach($pages as $page): ?>
        <li>
          <strong><?php echo $page['title']; ?></strong>
          <?php echo $page['description']; ?>
          <?php if ($patron->hasActivePledge() && $patron->getPledge()->amount_cents >= $page['minimum']) { ?>
            <a href="<?php echo $page['url']; ?>">Click here to access</a>
          <?php } else { ?>
            You need to pledge at least $<?php echo number_format($page['minimum'] / 100, 2); ?> to be
            eligible for this.
          <?php } ?>
        </li>
      <?php endforeach; ?>
    </ul>
  </body>
</html>

As you can see we are defining an array of pages, each of which has a minimum amount in cents required to access it. Then we’re looping through these pages, displaying their title and description and if the user has pledged enough then they are also shown the link, and if not they are told how much they’d need to pledge to have access.

Now you’re ready to create your individual pages, like settings.php:

<?php require_once __DIR__ . '/../bootstrap.php';

if (! isset($_SESSION['accessToken'])) {
    Header('Location: login.php');
    exit;
}

use Squid\Patreon\Patreon;

$patreon = new Patreon($_SESSION['accessToken']);
$patron = $patreon->me()->get();

// If they don't have an active pledge that is at least 1000 cents ($10) then
// redirect them back to the rewards page.
if (! $patron->hasActivePledge() || $patron->getPledge()->amount_cents < 1000) {
    Header('Location: index.php');
    exit;
}

?>
<html>
  <body>
    <h1>Settings</h1>
    <p>
      Hello! This is the settings page. You are only able to see this message
      because you pledge at least $10 per month. Thank you. Please see below
      for the functionality I have implemented.
    </p>
  </body>
</html>

And that’s it! Set your document root to the public folder and you now have a simple website that allows Patreon users to log in and see which pages they are eligible to access. Let me know if you need any further help if you do choose to take this approach :slight_smile:

1 Like