What's the best way to integrate the Patreon API into my serverless app

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.

I’m not completely clear on this. Are you looking for a way to pass something from the frontend to the backend?

Hey Jay,

Yeah that was the intent. I got it working enough that I should have received the token but it turns out that the API isn’t being maintained and just returns:

Warning: Links not implemented yet.
Warning: Links not implemented yet.
Warning: Links not implemented yet.
Warning: Links not implemented yet.
Warning: Links not implemented yet.
Warning: Links not implemented yet.
Warning: Links not implemented yet.
Warning: Links not implemented yet.
Warning: Links not implemented yet.
Warning: Links not implemented yet.
error! TypeError: Cannot read property 'end' of undefined

And there is no one home in the forums or the GitHub repo for these apis so I’m going to go in a different direction. There were several discussion threads regarding this same error from a year ago and the people in the conversation sound like they abandoned using this api as well.

The part that is confusing me still though is how is the event object defined? The instructions for this api was to use a call to their system to get a one-time code that is used to request the user token from the server. The documentation recommended making that call for the user token from the server and I wanted to just bounce it back to a lambda function to handle it without manipulating the url. But the call for the one time code returned it as:

url-route/?code=eobegoin230hefovniiWBRevone

and I couldn’t figure out for the life of me how to access a query string parameter from the event in a lambda function that wasn’t interacting with another AWS service.

I don’t know if I was using the right search terms but the documentation seems scarce in this area. Have you ever come across an issue like that and how did you solve it?

Yeah the documentation for this is poor but to get the query string parameters in Lambda use this:

event.queryStringParameters
1 Like