CRMScript and Oauth with bearer

Hello...

I am trying to do a GET request to a cloud API and getting authentication error. 
I do succeed in getting the access token and saving it for authorization in the GET request.

Is it in CRMScript a special way to authenticate here when you use bearer + token ?

 

HTTP getToken; 

String requestBody = "grant_type=client_credentials&client_id=client_id&client_secret=client_secret";

getToken.addHeader("Content-type", "application/x-www-form-urlencoded");

getToken.setOption('parameters', '?'+requestBody);
String result = String(getToken.post("https://api.com/token"));

print(result);

XMLNode x = parseJSON(result);

XMLNode[] info = x.getChildren();

String token = "bearer "+info[0].getText();


printLine(token);

  
HTTP getAllCustomers;
getAllCustomers.addHeader("Content-Type", "application/json");
getAllCustomers.addHeader("Authorization", token);

	//getAllCustomers.setValue("parameters",  "?"+requestBody);	


result = String(getToken.post("https://api.com/api/v1/customer"));

print(result);

//ERROR MESSAGE:
//{"Message":"Authorization has been denied for this request."}

 

RE: CRMScript and Oauth with bearer

Hello Ummair, 

Are you sure you are not just missing "Bearer" in front of the token? 

HTTP getAllCustomers;
getAllCustomers.addHeader("Content-Type", "application/json");
getAllCustomers.addHeader("Authorization","Bearer " + token);

result = String(getToken.post("https://api.com/api/v1/customer"));

 

//Eivind

By: Eivind Johan Fasting 30 May 2020

RE: CRMScript and Oauth with bearer

Hey Eivind.

I save it in the string a few lines up in the code.

String token = "bearer "+info[0].getText();

 

 

By: Ummair Tahir 30 May 2020

RE: CRMScript and Oauth with bearer

Hi,

Just to verify does it work if you have Bearer instead of bearer as Eivind suggests?

By: Michel Krohn-Dale 2 Jun 2020

RE: CRMScript and Oauth with bearer

Tried with both capital and lower case 'b' in bearer.... 

By: Ummair Tahir 2 Jun 2020

RE: CRMScript and Oauth with bearer

Hi,

Perhaps you should change this line

result = String(getToken.post("https://api.com/v1/customer");

to

result = String(getAllCustomers.post("https://api.com/v1/customer"));
By: Michel Krohn-Dale 2 Jun 2020

RE: CRMScript and Oauth with bearer

Ofcourse Michel. I need to use the same HTTP request to both acquire the token and the GET request. 
I had the thought of trying it, but forgot it. 

Thank you !

By: Ummair Tahir 2 Jun 2020