Patreon / patreon-php

Interact with the Patreon API via OAuth
Apache License 2.0
145 stars 41 forks source link

Struggling to get this working with Socialite/Laravel. #53

Closed sustained closed 4 years ago

sustained commented 5 years ago

I'm using Socialite for OAuth (instead of your OAuth client) and Laravel and I have that side of it working fine but I can't get this library to work at all.

  1. User goes to /connect/patreon/redirect and is redirected to Patreon.
  2. User clicks "allow".
  3. User is redirected to my site, /connect/patreon/callback.

At this point, I call Socialite::driver('patreon')->user() and get the following data (actual data snipped/replaced):

array : 8 [
    ▼
        "token" => "SNIPPED"
        "refreshToken" => "SNIPPED"
        "expiresIn" => 2678400
        "userId" => "123456"
        "nickname" => "foobar"
        "name" => "foobar"
        "email" => "foobar@test.com"
        "avatar" => "SNIPPED"
]

The data is all there and as I would expect it to be.

So then I try to use the token with your API library:

        try {
            $user = Socialite::driver($provider)->user(); // The expected data is all present, see above.
            $patreon = new API($user->token);
            $response = $patreon->fetch_user();
            $patron = $response->get('data');
            $pledge = null;

            if ($patron->has('relationships.pledges')) {
                $pledge = $patron->relationship('pledges')->get(0)->resolve($response);
                dd($pledge->getKeys());
            }
        } catch (\Exception $e) {
            dd($e);
        }

And I get this exception:

Art4 \ JsonApiClient \ Exception \ AccessException
"0" doesn't exist in this resource.

Stuff like this works fine though:

$patron->attribute('email');

What gives?

sustained commented 5 years ago

Ah, I think I know what is going on here.

Presuming that:

  1. $owner = the user who generated the api key/secret and owns the campaign.
  2. $user = the user who clicks "allow" after being redirected to Patreon.

...then relationships only shows the pledge from $user to $owner, if it exists.

It does NOT show all of $user's pledges.

Hope this helps anyone else who is having the same problem.

codebard commented 5 years ago

Art4 \ JsonApiClient \ Exception \ AccessException

There were backwards compatibility breaking change sin Art4 json library. To avoid such issues, Art4 json library was removed from the latest version which you can get from this repo (github). Which works with v2. I recommend you use that instead of the Art4 including old version.