0

Using the code given from Packagist (https://packagist.org/packages/patreon/patreon?q=&p=6), I am unable to get the expected results. My code now logs the user in, and returns their data (which I can view via var_dump), but I'm having issues actually reading it.

According to the Patreon API documentation, the data received from the API is automatically set as an array, unless specified otherwise. I'm running the exact code from their website but their API is returning an object, and I'm not sure how to read the user's data and pledge information from it. I've tried setting the return data as an array or json without any luck. I'm just getting this jumbled mess when I convert the API response into an array.

Screenshot - https://i.gyazo.com/3d19f9422c971ce6e082486cd01b0b92.png

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

use Patreon\API;
use Patreon\OAuth;

$client_id = 'removed';
$client_secret = 'removed';

$redirect_uri = "https://s.com/redirect";

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

$state = array();

$state['final_page'] = 'http://s.com/thanks.php?item=gold';

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

$href .= $state_parameters;

$scope_parameters = '&scope=identity%20identity'.urlencode('[email]');

$href .= $scope_parameters;

echo '<a href="'.$href.'">Click here to login via Patreon</a>';

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);
    $campaign_response = $api_client->fetch_campaign();

    $patron = $api_client->fetch_user();
    $patron = (array)$patron;
    die(var_dump($patron));
}

I want to be able to view the user's data and pledge information. I've tried things such as $patron->data->first_name, $patron['data']['first_name'], etc. which have all thrown errors about the index of the array not being found.

jelhan
  • 6,149
  • 1
  • 19
  • 35
  • Looks like it's returning a JSON object - have you tried [`json_decode()`](http://php.net/manual/en/function.json-decode.php) ? You're treating the result as if it's an object but looking at the response you need to treat it like a multi-dimensional associative array. – SierraOscar Feb 04 '19 at 22:53
  • @Sam using json_decode() on `$api_client->fetch_user()` returns `json_decode() expects parameter 1 to be string, object given in`. if i convert it to a string before using json_decode(), i get this: `Object of class Art4\JsonApiClient\Document could not be converted to string` – Anthony Cugat Feb 05 '19 at 00:20
  • Just had a quick scan through the API docs and I can’t see any `fetch_user` method - plus any calls to get user data require you pass an identifier if some kind for the user. Are you sure this call is doing what you expect? – SierraOscar Feb 05 '19 at 07:38

1 Answers1

0

You've probably already figured something out but I ran into the same thing and thought I'd share a solution here.

The JSONAPIResource object that the patreon library returns has a few specific methods that can return the individual pieces of data in a readable format.

import patreon
from pprint import pprint

access_token = <YOUR TOKEN HERE>   # Replace with your creator access token

api_client = patreon.API(access_token)
campaign_response = api_client.fetch_campaign()

campaign = campaign_response.data()[0]
pprint(campaign.id()) # The campaign ID (or whatever object you are retrieving)
pprint(campaign.type()) # Campaign, in this case
pprint(campaign.attributes()) # This is most of the data you want
pprint(campaign.attribute('patron_count')) # get a specific attribute
pprint(campaign.relationships()) # related entities like 'creator' 'goals' and 'rewards'
pprint(campaign.relationship('goals'))
pprint(campaign.relationship_info()) 
# This last one one ends up returning another JSONAPIResource object with it's own method: .resource() that returns a list of more objects 
# for example, to print the campaign's first goal you could use:
pprint( campaign.relationship_info('goals').resource()[0].attributes() )

Hope that helps someone!

Jase
  • 76
  • 5