0

使用 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'] 等,它们都抛出了关于找不到数组索引的错误。

4

1 回答 1

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() )

希望对某人有所帮助!

于 2019-08-15T04:02:13.413 回答