Best answer

Define secondary API call in Access token request

  • 24 July 2021
  • 1 reply
  • 224 views

Hi Folks,

 

I have defined computed field ”OrgId” in the step 1 of oauth authentication. 

Basically i want to save the orgId to the authData object. For which I tried adding a secondary call in the access token request endpoint. 

Now if I add a secondary call endpoint in the access token request endpoint then how do I merge the json response of both these respective calls?

I have to save the response to the bundle.authData of both the calls & use bundle.authData.orgId property that was created as a computed field in Step 1. 
This property will be available for any other action step as it will be saved in the authData.

 

Please help me with some example how can I add a secondary call to my test endpoint and merge the json response so that authdata has the response of token as well as response of my test API 

 

Thanks

icon

Best answer by Zane 27 July 2021, 23:07

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 7
Badge +9

Here’s a contrived example. Let’s say, using the GitHub API, we wanted to grab the user’s username and store it in the bundle.authData context to use in URL path params. 

First add a “computed” field to store the value:
 

 

Next, add an API request to your handler for the access token request. You’ll need to make these API requests serially, as you need to wait for the token request to complete so you have a token to use in the request to the user endpoint. When you get the result from the call to the user endpoint, pull out the “login” field and add it to the result and Zapier will store it the computed field and make it available in the authData context.

the code (think through error handling - this is not necessarily ready for production):

const tokenRequestOptions = {

  url: 'https://github.com/login/oauth/access_token',

  method: 'POST',

  headers: {

    'content-type': 'application/x-www-form-urlencoded',

    'accept': 'application/json'

  },

  params: {

  },

  body: {

    'code': bundle.inputData.code,

    'client_id': process.env.CLIENT_ID,

    'client_secret': process.env.CLIENT_SECRET,

    'grant_type': 'authorization_code',

    'redirect_uri': bundle.inputData.redirect_uri

  }

};


const tokenResponse = await z.request(tokenRequestOptions);

tokenResponse.throwForStatus();

const results = tokenResponse.json;

const userRequestOptions = {

  url: 'https://api.github.com/user',

  method: 'GET',

  headers: {

    'Authorization': `Bearer ${results.access_token}`

  },

  params: {

  }

}

const userResponse = await z.request(userRequestOptions);

results['login'] = userResponse.json.login;

return results;
 

You can reference your computed field like this:

 

Let us know if that doesn’t get you up and running.