401 Unauthorized Error on Identity GET

Hey fellas, I’m a relative newbie to working with PHP and HTTP so you’ll have to forgive me if I’ve missed something huge here - I’m working on a site that plans to integrate some per-tier functionality, so I need to be able to resolve a Patreon login, check the user’s tier and get them logged in a database, etc.

The login itself is working fine, I’m getting to the Patreon authorization page and it’s sending me back to my redirect URI after my POST with my Access Token and all. However, when I try to use that in the way I think the documentation is suggesting after looking over OAuth (it seems a little bit ambiguous at points):

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.patreon.com/api/oauth2/v2/identity');
$headers = ["Authorization: Bearer " . $_SESSION["access_token"]];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$apiResponseMemberData = curl_exec($ch);

It’s handing me back a 401 authorization error, suggesting I supplied bad credentials or that my browser didn’t know how to supply them. I thought a possible answer might be in setting

Content-Type: application/x-www-form-urlencoded

as another custom header to tell it how to supply the content, but doing so doesn’t seem to be doing the trick either.

Pardon me if there’s a super simple fix on this one, I was shopping around for a fix for a while but unfortunately the vagueness of the feedback itself is making it hard to know where to go looking for a fix. Thanks a bunch in advance for the help.

First, you should make sure that you are calling a v2 endpoint with a v2 app, and tokens granted to that v2 app and v2 user.

If you use v2 endpoint with v1 client or tokens or vice versa, you may encounter issues with some calls.

Best is to format the app you have or v2 to work with v2 endpoints.

1 Like

Ah, understood. I was seeing if I could make sense of it along the way but I’m sure in referencing the two versions against each other I got something mixed up. I was so dead-set on assuming that I had screwed up something syntactical that I hadn’t thought it might be that.

I’ll take a look at it when I get the chance and see what I can find out, thanks.

1 Like

That seems to have done the trick, after getting everything standardized for V2 it’s giving me the data I want. However, after specifying some resources it doesn’t seem to like the way I tried to encode my GET:

$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, 'https://www.patreon.com/api/oauth2/v2/identity?fields[User]=about,first_name,last_name,vanity,created');
$headers = ['Content-Type: application/x-www-form-urlencoded', "Authorization: Bearer " . $_SESSION["access_token"]];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//get response from API
$apiResponseMemberData = curl_exec($ch);

It’s returning an error complaining about the square brackets in my V2 resource request saying “Please ensure the request/response body is x-www-form-urlencoded.”, though as far as I know I’ve got it set in the header there.

Again, sorry for the rookie questions, finding the right direction to go in isn’t easy without the right feedback.

EDIT: Scratch that, the answer supplied in this thread gave me a usable solution. Thanks again!

1 Like