APIv2 php scopes identify help

$client_id = secret;
$client_secret = secret;

$redirect_uri = secret;


$scope = '&scope=identity?include=memberships,memberships.currently_entitled_tiers'; //This scope isn't even working. But I think it's what I should use?  
$correctScope = '&scope=identity'; //This scope give me nothing but the user it seems.


$ref = 'https://www.patreon.com/oauth2/authorize?response_type=code&client_id=' . $client_id . $scope .  '&redirect_uri=' . urlencode($redirect_uri);

$state = array();

$state['final_redirect'] = 'https://patreonwhiteraven.com/picture/index.php'; // Replace http://mydomain.com/locked-content with the url of the content to be unlocked at your site, or whever you want the user to be directed to after returning from Patreon login/confirmation

// Add any number of vars you need to this array by $state['key'] = variable value

// Prepare state var. It must be json_encoded, base64_encoded and url encoded to be safe in regard to any odd chars. When you receive it back, decode it in reverse of the below order - urldecode, base64_decode, json_decode (as array)

$state_parameters = '&state=' . urlencode( base64_encode( json_encode( $state ) ) );

// Append it to the url 

$ref .= $state_parameters;

echo '<a class=b href="'.$ref.'">Log in with patreon</a>';

I have this in my first page. And this in my redirect.

require_once __DIR__.'/vendor/autoload.php';
 
use Patreon\API;
use Patreon\OAuth;

$client_id = secret;     
$client_secret = secret;  

$redirect_uri = secret; 


// The below code snippet needs to be active wherever the the user is landing in $redirect_uri parameter above. It will grab the auth code from Patreon and get the tokens via the oAuth client

if ( $_GET['code'] != '' ) {
	
	// From this part on, its no different from oAuth login example. Just do whatever you need.
	
	$oauth_client = new OAuth($client_id, $client_secret);	
		
	$tokens = $oauth_client->get_tokens($_GET['code'], $redirect_uri);
	
	$access_token = $tokens['access_token'];
	$refresh_token = $tokens['refresh_token'];
	
	// Here, you should save the access and refresh tokens for this user somewhere. Conceptually this is the point either you link an existing user of your app with his/her Patreon account, or, if the user is a new user, create an account for him or her in your app, log him/her in, and then link this new account with the Patreon account. More or less a social login logic applies here. 
	
	// Here you can decode the state var returned from Patreon, and use the final redirect url to redirect your user to the relevant unlocked content or feature in your site/app.
	
}

// After linking an existing account or a new account with Patreon by saving and matching the tokens for a given user, you can then read the access token (from the database or whatever resource), and then just check if the user is logged into Patreon by using below code. Code from down below can be placed wherever in your app, it doesnt need to be in the redirect_uri at which the Patreon user ends after oAuth. You just need the $access_token for the current user and thats it.

// Lets say you read $access_token for current user via db resource, or you just acquired it through oAuth earlier like the above - create a new API client

$api_client = new API($access_token);

// Return from the API can be received in either array, object or JSON formats by setting the return format. It defaults to array if not specifically set. Specifically setting return format is not necessary. Below is shown as an example of having the return parsed as an object. If there is anyone using Art4 JSON parser lib or any other parser, they can just set the API return to JSON and then have the return parsed by that parser


// Now get the current user:
$current_member = $api_client->fetch_user();

// At this point you can do anything with the user return. For example, if there is no return for this user, then you can consider the user not logged into Patreon. Or, if there is return, then you can get the user's Patreon id or pledge info. For example if you are able to acquire user's id, then you can consider the user logged into Patreon. 

// For example, after redirecting the user to the final redirect url after the unified flow process earlier, you can check for user's membership at this point, and show/hide content or allow/disallow features depending on the result


$currently_entitled_amount_cents = $current_member['included'][0]['attributes']['currently_entitled_amount_cents'];

if($currently_entitled_amount_cents!=null){
    if($currently_entitled_amount_cents >= 1000){
        header("Location: Final redirect");
}

For some reason this isn’t working, I have been searching for all kinds of reasons why it doesn’t work. Reading the documents patreon provides. I have only used the examples Patreon have provided from their patreon-php-master. When I use the identify scope I get something back but when I try to get the first name of the user the site doesn’t work. What am I doing wrong? How can I read the currently_entitled_amount_cents from the $current_member?

Thankful for any tips or help.

I found the issue, the code works as it should but the vendor folder was installed in the wrong place. It is working now.

1 Like

You can also use the PHP lib as a drop-in without using composer if you need to do so.