nischal vasisth
nischal vasisth

Reputation: 167

Unable to fetch payload.access_token value after hitting salesforce using http connector. how to extract access_token value?

I am hitting salesforce which is giving me application/x-www-form-urlencoded JSON which looks like this

{"access_token":"00D5j0008yLhA!ARwAQBufl2Y6S1Tu3AU5zUWBCb442Nj2JqwWFOkm3AFL16CQZleLO.mnZwlDFttOMEnldbt_WqJBzhLrh1mgI1XEIvAN8sAM","instance_url":"https://cloudceitude-13a-dev-ed.my.salesforce.com","id":"https://login.salesforce.com/id/00D5j00000LhAEAU/0055j000004TZbAAAW","token_type":"Bearer","issued_at":"1668188564383","signature":"LQbWIODP8pqE+wD1yjgbVUBxMDI6YV1HjENZUeK/eoY="}

but when I am trying to access value of access_token using payload.access_token it is giving me error

enter image description here

I have tried by transforming into JSON using Transform connector using

%dw 2.0
output application/json
​---
payload

still after I tried to access my token it shows same error

enter image description here

and also while debugging map function is not working while working on Payload in DataWeave but instead when I use MapObject It reflect this:

key=%7B%22access_token%22%3A%2200D5j000008yLhA%21ARwAQIN5UGZWtk8Ucu8MUNgbnHAKleeqAk2M73Afy9iRKNEKRppu4C7Drfi0hg8q5t8C.PYX7RsMMXQPsjxmpVB1Ev_pU.2p%22%2C%22instance_url%22%3A%22https%3A%2F%2Fcloudcertitude-13a-dev-ed.my.salesforce.com%22%2C%22id%22%3A%22https%3A%2F%2Flogin.salesforce.com%2Fid%2F00D5j000008yLhAEAU%2F0055j000004TZbAAAW%22%2C%22token_type%22%3A%22Bearer%22%2C%22issued_at%22%3A%221668330168420%22%2C%22signature%22%3A%22kkDnnUe9pZfiqiuEjRi02az0lwpAlmwmv3cMRmF9nRU

Upvotes: 0

Views: 178

Answers (2)

Robert
Robert

Reputation: 1

I got the same error with keycloak authorization.

The problem is the response, which is the object with application/x-www-form-urlencoded type, so you can't get any payload attributes. This also causes problems with transformation, so probably all dw:Core functions and just transformations by datavawe won't work properly.

For example write(payload,"application/json") returns json with one attribute where key is whole reposne and value is null.

"{\n  \"{\\\"access_token\\\":\\\"eyJhbGciOiJS... \\\"scope\\\":\\\"email profile\\\"}\": null\n}"

Workaround that works for me is just set payload with JSON MIME Type.

AnypointStudio Set Payload example

Upvotes: 0

aled
aled

Reputation: 25664

I'll make an educated guess that the application or the server are somehow forcing the media type of the response to be application/x-www-form-urlencoded when it really is a JSON. The request for a token should be application/x-www-form-urlencoded, the response should be a JSON (application/json). If the server is returning it you should check with the administrators of that server. However if the application is overriding the content type of the response you should fix it.

Upvotes: 1

Related Questions