Trying to connect to the API using vanilla PHP

Hello. I am creating my first test app trying to learn how to use the API. I have already registered my client, made my log in button, got my code and state after successfully login-in thru Patreon, and now I’m trying to validate the receipt of the OAuth token. I am using php’s cURL like this:

<?php
$myCode=$_GET['code'];
$myState=$_GET['state'];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.patreon.com/api/oauth2/token?Content-Type: application/x-www-form-urlencoded" );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "code=$myCode
&grant_type=authorization_code
&client_id=//my client id
&client_secret=//my client secret
&redirect_uri=//my redirect pointed again to this php file");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output= curl_exec($ch);

curl_close($ch);

echo $server_output
?>

I understand that if successful this should give me back a JSON object. However what I get is:
{“error”: “unsupported_grant_type”}

I am not very experienced yet, so I may have made mistakes that I’m not aware of. Most of the Patreon docs are pretty impenetrable to me, so I’m coming here looking for help. Any hint on where to go next would be most appreciated.
Thanks!!

How about trying the PHP lib or taking out code from there to help you do what you want to do?

uh… thanks but already tried that, and found roadblocks there, mainly how do I install those libraries on the server where my page is running? My choice is php but I understand that I have to use composer to install it and my C-panel does not have composer so I have to install composer there first but what I have found on that subject seems to be for advanced apache developers with experience using the command line which is not my case. In that respect I found the documentation on the Patreon API very unfriendly, that’s why I choose this route. Could you point me to a better resource?

mainly how do I install those libraries on the server where my page is running

Just ftping it to a subfolder and including it does not work?

I have to use composer to install it

That was long ago. Since a while ago the lib was modified to work with or without composer. The package at packagist is the old one, however, and it shouldnt be used.

Id recommend just downloading it from github, unpacking it and uploading it to your site in a folder you prefer. Then including it from above and trying it out.

Thanks! That was much easier than I thought at first. Now I feel dumb for having asked. I’m working on it right now.

1 Like

Good luck with your app.

1 Like