Warik
January 28, 2020, 9:02pm
1
Hi there.
I’m just starting out with the php API, and using the following unified example and getting the user details below I’ve managed to get it all working, but I am having issues trying to access the membership tier data, I’m struggling with the scopes.
<?php
//
// This example shows the best way to have users unlock content or features at your site/app via Patreon
//
/*
Unified flow is a flow at Patreon to which you can send users to unlock a feature or a piece of content at your site/app.
You provide a $ amount to ask for pledge from the user, and a final redirect url to return the user to.
Patreon handles the rest - if the user is not logged in or registered, asks the user to register or login. If the user is not your patron, asks the user to become your patron. If the user doesnt have enough pledge, asks the user to pledge more.
All happen in conjunction - if user is not logged in, then s/he is first asked to login, and then asked to pledge etc. All in a single, smooth, unified flow.
After this process is complete, the user is redirected to your oAuth redirect url. The final redirect url you sent in your state vars is also returned.
After this, you can grab the user info, grab the final redirect url and redirect the user to that url in your site/app.
This file has been truncated. show original
<?php
// This example shows you how to get details of the current user and other info like active membership which the user has
// We assume that you already logged the user in via Patreon to get the access token, or already logged in the user via Patreon before, and got and saved the access token, and reading it from wherever you saved it for this user
// We assume you put the access token into an $access_token var
require_once __DIR__.'/vendor/autoload.php';
use Patreon\API;
use Patreon\OAuth;
// Start new Patreon API client
$api_client = new API($access_token);
// Fetch the user's details
$current_member = $api_client->fetch_user();
// $current_patron now has the user details. The return should match the documentation for /identity return at https://docs.patreon.com/#get-api-oauth2-v2-identity which should be augmented by additionally requested scopes.
This file has been truncated. show original
Can anyone throw me a lifeline, I’m not a developers developer just a bit of a hack.
cheers
First, you should make sure that you are using a v2 client and you are calling a v2 endpoint with it.
The scopes - you should request them at the time which you are sending the user to Patreon to log in via Patreon.
Warik
January 28, 2020, 10:15pm
3
Hey @codebard ,
My client api is v1, but it seems as though I cant edit it.
Any Ideas?
Trying a v2 client and calling v2 api may provide improvement - v1 api is going to be deprecated in foreseeable future, therefore it may not be providing everything which v2 api offers.
Warik
January 28, 2020, 10:57pm
6
@codebard these are the scope_parameters I am using, looking for the tiers.
$scope_parameters = ‘&scope=campaigns.members%20identity%20identity’.urlencode(’[email]’);
$include_parameters = “&include=campaign%20memberships”;
$href = ‘https://www.patreon.com/oauth2/become-patron?response_type=code&min_cents= ’ . $min_cents . ‘&client_id=’ . $client_id . $scope_parameters . $include_parameters . ‘&redirect_uri=’ . $redirect_uri;
then
$api_client = new API($access_token);
$current_member = $api_client->fetch_user();
$email = $current_member[‘data’][‘attributes’][‘email’];
print_r($current_member);
This works, But I just cant find the currently entitled tiers…
That url looks like an authorization url. When you get the authorization token with correct scopes, you must call the api endpoint with the correct includes you desire - derived from those scopes.
So first send the user to the Patreon login to get authorization codes. Or via the pledge flow. When you get the tokens via the codes, you can call the relevant endpoint and request relevant includes.
Also, check this out:
https://docs.patreon.com/#get-api-oauth2-v2-campaigns-campaign_id-members
Warik
January 29, 2020, 12:47am
8
@codebard Thank you for responding so quickly, yes that is the authorization url, it then redirects to page with this code below.
But this is where it get real confusing for me.
I don’t even know if i’m on the right path here…
$client_id = ‘my id’; // Replace with your data
$client_secret = ‘my scret’; // Replace with your data
$redirect_uri = “redirect url”;
if (isset($_GET[‘code’])) {
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’];
$api_client = new API($access_token);
$current_member = $api_client->fetch_user();
$email = $current_member[‘data’][‘attributes’][‘email’];
$first_name = $current_member[‘data’][‘attributes’][‘first_name’];
$last_name = $current_member[‘data’][‘attributes’][‘last_name’];
}
Warik
January 29, 2020, 7:57pm
9
Rightio,
I’ve found out what I need to. I didn’t know the other fetch functions from the api… ie fetch_campaign_details etc.
Thanks for you help
1 Like