Five Stars

Pass dynamic authorization token to tRest component

Hi,

 

I need to pass authorization token coming from one api to another api using tREST component. When I try to use context variable to store the token and use it in tREST component it is failing while working when hard coding the token can u please help.

 

Error_API.JPG

  • Talend Integration Cloud
15 REPLIES
Eleven Stars

Re: Pass dynamic authorization token to tRest component

Each implementation of OAuth is slightly different (annoyingly), but I suspect that you might be missing either a token prefix or maybe converting it to base64 (or similar). I have written a tutorial on using Google's OAuth implementation with Talend here (https://www.rilhia.com/tutorials/using-oauth-20-talend-access-google-apis) and recall having all sorts of irritating issues like this.

Rilhia Solutions
Five Stars

Re: Pass dynamic authorization token to tRest component

Hi,



I tried the above solution but it is not working, please suggest another way.


Eleven Stars

Re: Pass dynamic authorization token to tRest component

Hi @pravin_sanadi,

 

You will need to give a bit more information as to what is going wrong. Do you receive any error messages or XML responses which describe what it is objecting to? We really do need the details to help with this

 

Regards

 

Richard

Rilhia Solutions
Five Stars

Re: Pass dynamic authorization token to tRest component

Hi,



It is giving me unauthorized access error. When I hardcode the value for Authorization it works fine.

Following is the output captured in tLogRow from tREST component.


+----------.
| tLogRow_3 |
|=----------------------------------------------------------------------------------------------------------------------------------------+---------=|
|Body |ERROR_CODE|
|=----------------------------------------------------------------------------------------------------------------------------------------+---------=|
|{"status":"failure","data":{"entityCode":null,"fieldCode":null,"errorCode":"UNAUTHORIZED_REQUEST","errorParams":null,"fieldErrors":null}}|401 |

'-----------------------------------------------------------------------------------------------------------------------------------------+----------'


Eleven Stars

Re: Pass dynamic authorization token to tRest component

Can you post a screenshot of your job and how you are setting the value of the context variable.

Rilhia Solutions
Community Manager

Re: Pass dynamic authorization token to tRest component

I suspect you don't assign the value to context variable correctly, I have achieved similar case and I am able to use context variable, please upload a screenshot to show where/how you assign the value, and show an example value. 

 

Regards

Shong

----------------------------------------------------------
Talend | Data Agility for Modern Business
Five Stars

Re: Pass dynamic authorization token to tRest component

Hi,



This is the job structure

[cid:image001.png@01D32264.3FD47AC0]



First tREST_1 is gives the token . In tJavaRow_1 I am setting the context variable

[cid:image002.png@01D32264.3FD47AC0]





And in tRest_2

[cid:image003.png@01D32264.3FD47AC0]






Five Stars

Re: Pass dynamic authorization token to tRest component

Hi,
This is the job structure

Error_1.jpg

First tREST_1 is gives the token . In tJavaRow_1 I am setting the context variable

Error_2.jpg

 

And in tRest_2

 

Error_3.jpg

 

Eleven Stars

Re: Pass dynamic authorization token to tRest component

You are printing the token value out to the output window once it is set in your context variable. Can you see this in the output window? If you can, can you change the variable you are printing from var_FINAL_TOKEN to your context variable? Does that print out correctly? Can you show us what is returned?

 

If everything is being returned as you would expect, try changing your onComponentOK link to an OnSubJobOK link

Rilhia Solutions
Five Stars

Re: Pass dynamic authorization token to tRest component

Hi,

Please find below the screenshot

 

Error_4.JPG

 

Output

Error_6.JPGError_5.JPG

 

After tJavaRow the component does not gives OnSubJobOK link

Eleven Stars

Re: Pass dynamic authorization token to tRest component

That looks like a rather large "token", but I am assuming that you simply copied and pasted it when you tried your hardcoded solution which worked? I am also assuming you tested this by printing the value of the context variable. If so, this sounds like a timing issue. I suspect that the tRest component is being triggered to start it's initialization code before the context variable is set. To get round this you can use the OnSubJobOK link as I suggested. However, I wasn't explicit last time. You need to link this from the first tRest to the second tRest. Give that a go

Rilhia Solutions
Five Stars

Re: Pass dynamic authorization token to tRest component

but I am assuming that you simply copied and pasted it when you tried your hardcoded solution which worked?

Yes, it worked with just copy paste.



I tried with SubJobOk too, no luck Smiley Sad




Eleven Stars

Re: Pass dynamic authorization token to tRest component

I can't believe I missed this.....I think I have spotted your issue. You are setting your token like this....

 

String var_FINAL_TOKEN = "\"Bearer "+var_TOKEN_1[0].replaceAll("\"","")+"\"";

That will create a String which starts and ends with a quote. Your service will not want this. Simply remove the \" from before Bearer and the +"\"" from the end of the line. 

 

The reason this worked when you copied and pasted it is that with the quotes surrounding the text, turn this into a literal String when you paste it into the header field.

 

Rilhia Solutions
Five Stars

Re: Pass dynamic authorization token to tRest component

Hi,



Thanks a lot it worked. It was indeed timing issue. The OnSubJobOk and removal of quotes worked. Thanks a lot Smiley Happy


Eleven Stars

Re: Pass dynamic authorization token to tRest component

You can probably go back to your original structure. The issue would have been caused by the quotes. There being a timing issue as well is unlikely....but not impossible :-)

Rilhia Solutions