首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从Patreon JSON API读取数据

无法从Patreon JSON API读取数据
EN

Stack Overflow用户
提问于 2019-02-05 06:33:04
回答 1查看 524关注 0票数 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

代码语言:javascript
复制
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‘等,它们都抛出了有关未找到数组索引的错误。

EN

回答 1

Stack Overflow用户

发布于 2019-08-15 12:02:13

你可能已经弄明白了一些事情,但我也遇到了同样的事情,我想我应该在这里分享一个解决方案。

patreon库返回的JSONAPIResource对象有几个特定的方法,可以以可读的格式返回各个数据段。

代码语言:javascript
复制
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() )

希望这对某些人有帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54525330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档