使用Packagist(https://packagist.org/packages/patreon/patreon?q=&p=6)提供的代码,我无法获得预期的结果。我的代码现在将用户登录,并返回他们的数据(我可以通过var_dump查看),但是实际上在读取它时遇到了问题。
根据Patreon API文档,除非另有说明,否则从API接收的数据将自动设置为数组。我正在从他们的网站上运行确切的代码,但是他们的API返回一个对象,而且我不确定如何读取用户的数据并从中保证信息。我尝试将返回数据设置为数组或json,但没有任何运气。当我将API响应转换为数组时,我只是一团糟。
屏幕截图-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));
}
我希望能够查看用户的数据和质押信息。我已经尝试过诸如$ patron-> data-> first_name,$ patron ['data'] ['first_name']等之类的东西,这些东西都引发了关于找不到数组索引的所有错误。
答案 0 :(得分:0)
您可能已经发现了一些问题,但是我遇到了同样的事情,并认为我会在这里分享解决方案。
patreon库返回的JSONAPIResource对象具有一些特定方法,这些方法可以以可读格式返回单个数据。
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() )
希望对某人有帮助!