1

我正在尝试在一个项目中实现 Patreon API,但遇到了障碍。在被重定向回我的站点后,我能够登录并接收授权码,但由于某种原因,应该在其中包含访问和刷新令牌的数组为空。

我通过作曲家https://packagist.org/packages/patreon/patreon使用 patreon 包

所以我的代码顶部似乎工作正常。但是,如果我完全失明并且这是我将其包括在内的问题。


$client_id = 'xxxxxxxxxx';      // Replace with your data
$client_secret = 'xxxxxxxxx';  // Replace with your data


$redirect_uri = "http://localhost/Final%20API/thankyou.php"; 

// Generate the oAuth url

$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://localhost/Final%20API/'; 

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

// Append it to the url

$href .= $state_parameters;


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

$href .= $scope_parameters;

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

Notice: Trying to access array offset on value of type null in C:\MAMP\htdocs\patreon3.php on line 69

Notice: Trying to access array offset on value of type null in C:\MAMP\htdocs\patreon3.php on line 70

我得到了 code= 返回,所以我试图用它来获取我的令牌,但我空手而归,我不知道我在这里做错了什么。

if ( $_GET['code'] != '' ) {
    $code = $_GET['code'];
    $oauth_client = new OAuth($client_id, $client_secret);  

    $tokens = $oauth_client->get_tokens($code, $redirect_uri);
    
    $access_token = $tokens['access_token'];
    $refresh_token = $tokens['refresh_token'];

我认为症结在于 if 语句是否正确?如果是这样,我该怎么做才能理顺它?

4

1 回答 1

0

你能检查一下$tokens里面有什么吗?
在这条线之后
$tokens = $oauth_client->get_tokens($code, $redirect_uri);

添加这个
var_dump($tokens); 你可能会得到一些错误

于 2021-08-25T11:00:58.950 回答