Hi, I am working on an OAuth api integration with a service called patreon. There is a part in it where I have to make a backend call to their service once I get a one-time code from the frontend and then use that one-time code to make a call from the server to patreon in order to receive more sensitive tokens that I will eventually have to pass back down to the client in the response body.
The code example below is provided on the patreon node module page.
import url from 'url'
import { patreon as patreonAPI , oauth as patreonOAuth } from 'patreon'
const CLIENT_ID = 'pppp'
const CLIENT_SECRET = 'pppp'
const patreonOAuthClient = patreonOAuth(CLIENT_ID, CLIENT_SECRET)
const redirectURL = 'http://mypatreonapp.com/oauth/redirect'
function handleOAuthRedirectRequest(request, response) {
const oauthGrantCode = url.parse(request.url, true).query.code
patreonOAuthClient
.getTokens(oauthGrantCode, redirectURL)
.then(tokensResponse => {
const patreonAPIClient = patreonAPI(tokensResponse.access_token)
return patreonAPIClient('/current_user')
})
.then(({ store }) => {
// store is a [JsonApiDataStore](https://github.com/beauby/jsonapi-datastore)
// You can also ask for result.rawJson if you'd like to work with unparsed data
response.end(store.findAll('user').map(user => user.serialize()))
})
.catch(err => {
console.error('error!', err)
response.end(err)
})
}
The biggest question I have is what is the best way to pass the one time code in the event object and assign it to the variable oauthGrantCode
?
I tried using event.pathparameter.code
and adding that to my get-event.json to invoke locally but it isn’t working for me. I get an error TypeError: Cannot create property 'events' on string 'handler.main'
.
Any ideas what I should be doing here? Thanks in advance.