I’m trying out OAuth using Patreon’s api. I’ve very new to the OAuth process and had been using Patreon’s Javascript Package to help manage the request for me.
- NPM: patreon - npm
- Patreon Documentation: API Reference
So far I’ve been able to successfully get the token via:
import * as patreon from 'patreon';
const patreonOAuthClient = patreon.oauth(clientId, clientSecret);
patreonOAuthClient.getTokens(oauthGrantCode, redirectURL).then((tokenResponse) => {
console.log(tokenResponse);
})
The token I recieve comes out like this:
// Example Token from getTokens()'s then()-response
tokenResponse = {
access_token: "UbHYT3H51GpeYueBeBuvBj1fnEFzv5A5870s_rYeMHo",
expires_in: 2678400,
refresh_token: "AP5aAw-gJbVf35tWxQb74rmJJz2MhwIYq660m0jiZQ4",
scope: "my-campaign pledges-to-me users",
token_type: "Bearer",
version: "0.0.1"
}
In my local server, I’m trying to get refresh token to work so I don’t have to keep asking users permission every month.
Although when I use the refresh token method I get a 400 Bad Request:
patreonOAuthClient.refreshToken(tokenResponse).then(response => {
console.log(response, 'success!');
}).catch(err => {
console.log(err, ':(');
});
It’s not shown in the npm documentation but you can find refreshToken() on the github source code of patreon.
According to here in their api documents:
If you wish to get up-to-date information after the token has expired,
a new token may be issued to be used for the following month. To
refresh a token, make a POST request to the token endpoint with a
grant type of refresh_token, as in the example. You may also manually
refresh the token on the appropriate client in your clients page.
So is the reason I’m getting 400 because I need to wait a month to refresh the token or am I just incorrectly implementing the API? I’m hoping someone with more OAuth experience can tell me if we should be doing token refreshes either before or after the token expires?
(If you refresh it before it expires is there a certain way to time an express server to do it before the month expires? As I think it adding a timeout for each token would be really bad for memory).