Best answer

Bearer token App Integration - authorisation failure

  • 20 February 2024
  • 2 replies
  • 77 views

Userlevel 1
Badge

Hi,

I am trying to create an integration where the authorisation request that the API client request a token, then that token is used in a Bearer token for subsequent requests. As far as I understand, the Session Authentication type should be used of this, however, I am causing a 500 server error via Zapier. I have a successful authorisation mechanism setup in Postman, but can’t seem to replicate this in Zapier.

The API endpoint I am looking to use to request the token is https://cloudshare.stoplight.io/docs/AccelerateAPI/5jzyft1s3p00h-authentication

In Postman, the code I use to request the token is:

pm.sendRequest({
url: 'https://api.accelerate.cloudshare.com/v4/oauth/token',
method: 'POST',
header: 'Content-Type: application/x-www-form-urlencoded',
body: {
mode: 'urlencoded',
urlencoded: [
// The CloudShare Oauth2 implemntaiton is the same as using "Client Credentiatls" in the built in Postman Oauth2 authorisation
{ key: "client_id", value: pm.variables.get("API_ID"), disabled: false },
{ key: "client_secret", value: pm.variables.get("API_KEY"), disabled: false },
{ key: "grant_type", value: "client_credentials", disabled: false }
]
}
}

This result in a body that is sent similar to the following:

 

In Zapier, I switched to use the code view for the authentication, and even hard coded the `client_id` and `client_secret` values (as I couldn’t get the form parameter to work) but I could not get this to work in any case. It now looks like:

 

Test the authentication I see:

 

authentication failed: The app returned "Action failed". What happened (You are seeing this because you are an admin): Stack trace: ResponseError: {"status":500,"headers":{"content-type":"application/json; charset=utf-8","retry-after":null},"content":"{\"message\":\"Action failed\",\"code\":\"0x50001\"}","request":{"url":"https://api.accelerate.cloudshare.com/v4/oauth/token"}} at _throwForStatus (/var/task/node_modules/zapier-platform-core/src/http-middlewares/after/prepare-response.js:13:11) at outResp.throwForStatus (/var/task/node_modules/zapier-platform-core/src/http-middlewares/after/prepare-response.js:68:5) at throwForStatusMiddleware (/var/task/node_modules/zapier-platform-core/src/http-middlewares/after/throw-for-status.js:5:14) at Object.<anonymous> (/var/task/node_modules/zapier-platform-core/src/middleware.js:80:37) From previous event: at /var/task/node_modules/zapier-platform-core/src/middleware.js:77:26 at Array.reduce (<anonymous>) at afterMiddleware (/var/task/node_modules/zapier-platform-core/src/middleware.js:76:21) at Object.<anonymous> (/var/task/node_modules/zapier-platform-core/src/middleware.js:94:18) From previous event: at Object.<anonymous> (/var/task/node_modules/zapier-platform-core/src/middleware.js:92:10) From previous event: at /var/task/node_modules/zapier-platform-core/src/middleware.js:89:45 at /var/task/node_modules/zapier-platform-core/src/tools/create-lambda-handler.js:212:18 at bound (node:domain:433:15) at runBound (node:domain:444:12) at process.processImmediate (node:internal/timers:476:21) at process.topLevelDomainCallback (node:domain:161:15) at process.callbackTrampoline (node:internal/async_hooks:126:24) From previous event: at Domain.<anonymous> (/var/task/node_modules/zapier-platform-core/src/tools/create-lambda-handler.js:196:10) at Domain.run (node:domain:389:15) at Runtime.handler (/var/task/node_modules/zapier-platform-core/src/tools/create-lambda-handler.js:192:19) at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)

 

Any thoughts?

 

One the raw access token retravl is working, the next step to to use the response JSON that contains the `access_token` as a Bearer token with follow up requests (then refresh the token every 300 seconds as required).

 

icon

Best answer by swinster 21 February 2024, 12:51

View original

2 replies

Userlevel 1
Badge

Actually, I have already fixed the authentication issue - I’m not sure exactly what I did wrong, but I switched back to form view, and reset the request body parameter values, and it started working (well, the authentication part). 

 

 

 

The issue now is (I think) that the returned `access_token` doesn’t appear to be a variable that can be used:

We hit an error adding your new account
authentication failed: What happened (You are seeing this because you are an admin): Starting GET request to https://api.accelerate.cloudshare.com/v4/sales Received 401 code from https://api.accelerate.cloudshare.com/v4/sales after 116ms Received content "" Console logs: Stack trace: RefreshAuthError at throwForStaleAuth (/var/task/node_modules/zapier-platform-core/src/http-middlewares/after/throw-for-stale-auth.js:10:11) at Object.<anonymous> (/var/task/node_modules/zapier-platform-core/src/middleware.js:80:37) at bound (node:domain:433:15) at Object.runBound (node:domain:444:12) at Object.tryCatcher (/var/task/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/var/task/node_modules/bluebird/js/release/promise.js:547:31) at Promise._settlePromise (/var/task/node_modules/bluebird/js/release/promise.js:604:18) at Promise._settlePromise0 (/var/task/node_modules/bluebird/js/release/promise.js:649:10) at Promise._settlePromises (/var/task/node_modules/bluebird/js/release/promise.js:729:18) at _drainQueueStep (/var/task/node_modules/bluebird/js/release/async.js:93:12) at _drainQueue (/var/task/node_modules/bluebird/js/release/async.js:86:9) at Async._drainQueues (/var/task/node_modules/bluebird/js/release/async.js:102:5) at Async.drainQueues [as _onImmediate] (/var/task/node_modules/bluebird/js/release/async.js:15:14) at process.processImmediate (node:internal/timers:476:21) at process.topLevelDomainCallback (node:domain:161:15) at process.callbackTrampoline (node:internal/async_hooks:126:24)

 

Looking at the code view for step 2, the `access_token` is returned to a `sessionkey` variable, but I’m not sure how to reference this in the next step:

 

 

Userlevel 1
Badge

Ok, looks like I have resolved this💥. The way I was referring to the returned parameter using just `sessionKey` was incorrect. The sessionKey is added to the `bundle.authData` object, so need to be referenced as `bundle.authData.sessionKey`

FWIW, I also referenced the returned JSON incorrectly in my code above (not sure what happened there as I think it was right at one point). The authentication endpoint return a key named `access_token` parameter, which is then assigned to the `sessionKey` parameter.

So, using the Code view, I now have:

Step 2:

 

Step 3:

 

 

And flipping back to form view I have:

Step 2:

 

Step 3:

 

Reply