1

I'm trying to implement the Patreon API on a project and I've hit a roadblock. I'm able to sign in and receive an authorization code after being redirected back to my site, but for some reason the array that is supposed to have the access and refresh tokens in them is null.

Im using the patreon package via composer https://packagist.org/packages/patreon/patreon

So the top of my code seems to be working fine. However, in case I'm completely blind and it is the issue I'm including it.


$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

I get the code= returned so i'm trying to use it to get my tokens, but I'm coming up empty-handed and I don't know what I'm doing wrong here.

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'];

Am I correct in thinking that the sticking point is that if statement? If so, what do I have to do to straighten it out?

  • But why you don't tell us what line number 69-70 are? And frankly speaking ... on `http://localhost` you will never see an online web-server post back, unless having DNS & NAT set up. – Martin Zeitler Aug 20 '21 at 20:54
  • Sorry, about that. this is line 69 and 70 $access_token = $tokens['access_token']; $refresh_token = $tokens['refresh_token']; thanks for getting back to me about the localhost as well. I'll look into that right away. – ewbst11-web Aug 20 '21 at 21:07

1 Answers1

0

Can you check what is inside $tokens?
After this line
$tokens = $oauth_client->get_tokens($code, $redirect_uri);

Add this
var_dump($tokens); You propably get some error