Patreon / patreon-js

Use the Patreon API via OAuth.
MIT License
151 stars 30 forks source link

Bad Header trying to get current user's pledge #25

Closed GitFlip closed 6 years ago

GitFlip commented 6 years ago

I've been playing around with this API for a little bit now and I was able to successfully implement oauth and get the current user via /current_user. When I print the user JSON I was very disappointing to see only the following information about a pledge:

{ data: [ { type: 'pledge', id: '<pledge_id>' } ] }

Having only the ID isn't very helpful as I need to see the user's reward tier.

Now following the example in the readme, it seems that I can grab extra fields:

    const vPatreonAPI = patreon(vToken.access_token);
    const vUrl = jsonApiURL(`/current_user`, {
      fields: {
        pledge: [...pledge_schema.default.default_attributes, pledge_schema.default.attributes.total_historical_amount_cents]
      }
    })

    return vPatreonAPI(vUrl);

This apparently creates the following URL: 'https://www.patreon.com/api/oauth2/api/current_user?fields[pledge]=amount_cents,declined_since,created_at,pledge_cap_cents,patron_pays_fees,total_historical_amount_cents&'

But this returns a 400 - BAD REQUEST:

{ error:
   Body {
     url: 'https://www.patreon.com/api/oauth2/api/current_user?fields[pledge]=amount_cents,declined_since,created_at,pledge_cap_cents,patron_pays_fees,total_historical_amount_cents&',
     status: 400,
     statusText: 'BAD REQUEST',
     headers: Headers { _headers: [Object] },
     ok: false,
     body:
      PassThrough {
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     bodyUsed: false,
     size: 0,
     timeout: 0,
     _raw: [],
     _abort: false },
  response:
   Body {
     url: 'https://www.patreon.com/api/oauth2/api/current_user?fields[pledge]=amount_cents,declined_since,created_at,pledge_cap_cents,patron_pays_fees,total_historical_amount_cents&',
     status: 400,
     statusText: 'BAD REQUEST',
     headers: Headers { _headers: [Object] },
     ok: false,
     body:
      PassThrough {
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     bodyUsed: false,
     size: 0,
     timeout: 0,
     _raw: [],
     _abort: false } }

Is there a complete example somewhere on how to get the current user's pledge?

jrsun commented 6 years ago

I'm guessing that the issue might be the extra & at the end of your URL. You also might need to URL encode the square brackets.

By the way, have you looked at the top-level included key in the response? It also sounds like you might be looking at the relationships, which intentionally only include the type and id.

For more info, the JSONAPI spec might be useful.

GitFlip commented 6 years ago

Hi @jrsun! Thanks for the reply. The other day I just figured out there was the top-level response that I completely missed. To be honest, I don't think it's very clear in the API documentation. It might be nice for example if it showed that in the same promise for /current_user you can not only do: store.findAll('user').map(user => users.serialize()), but also store.findAll('pledge').map(user => pledges.serialize())

Also, you were 100% correct, I needed to URL encode the square brackets, which I also found out shortly after I learned about the top-level response. Again, this is not documented anywhere as far as I can tell. Something so simple, yet frustrating when you're first learning could really save some time. So hopefully this issue will help others in the future.

In the end the issue can be closed.

jrsun commented 6 years ago

Thanks for your feedback. It seems the URL encoding issue has bitten a lot of folks, so we'll amend the documentation to be clearer about that. It sounds like it may be worth including some info about the JSONAPI data format as well.