Forums | Back to Concur Developer Center

Header authorization error due to wrong format


#1

We are trying to get concur invoice URI using outh2 authentication. But not able to get it it by Code, where as from postman its working fine.
Please find the steps I am following.

Step 1. Get access token.

onInit: function() {
	var accessToken;
	var url;

	var settings = {
			"async": false,
			"crossDomain": false,
			"url": "https://us-impl.api.concursolutions.com/oauth2/v0/token",
			"method": "POST",
			"headers": {
				"Content-Type": "application/x-www-form-urlencoded"				    
			},
		"data": "grant_type=password&username=TTTTTTT&password=PPPPPPP&client_id=CCCCCC&client_secret=SSSSSS"
};

	$.ajax(settings).done(function (response) {
		console.log(response);
		accessToken = response.access_token;									
	});	

Result is fine getting the access token.

Step 2. Call invoice api to get the uri using access token from step 1

var auth = “Bearer” + " " + accessToken;

	var settings1 = {
							
			  "async": false,
			  "crossDomain": false,
			  "url": "https://implementation.concursolutions.com/api/image/v1.0/invoice/E84008C1838979851B17B",
			  "method": "GET",
			  "headers": {
				  "Authorization": auth,			    	
			  }
			};

			$.ajax(settings1).done(function (response1) {
			  console.log(response1);
			  url = response1.id;
			  console.alert(response1.id);
			});	

response I am getting is -

IDX10708: ‘System.IdentityModel.Tokens.JwtSecurityTokenHandler’ cannot read this string: ‘undefined’.
The string needs to be in compact JSON format, which is of the form: ‘..<OPTIONAL, Base64UrlEncodedSignature>’.2018-11-24T04:42:034B5BC986-F60C-425B-9BF6-1E157874A0A2

Step 3. Call invoice api to get the uri using access token from step 1 giving success response if we hardcode the access token by taking the token from first step.

var auth = "Bearer " + “eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IjE0NTU2MhuyQwMjIifQ.eyJjb25jdXIuc2NvcGVzIjpbIm9wZW5pZCIsInVzZXJfcmVhZCIsIkFUVEVORCIsIkNPTkZJRyIsIkNPTlJFUSIsIkVSRUNQVCIsIkVWUyIsIkVYUFJQVCIsIkNDQVJEIiwiQkFOSyIsIkVYVFJDVCIsIkZJU1ZDIiwiRk9QIiwiR0hPU1QiLCJJTUFHRSIsIklOU0dI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cGSqiLxQQ3ZJd7pHFemHxCedkRDQ6P8coRWqclfbZfVcLHYaPlshwh-HMrR_bZwfN_ziZnY7J2p_fma_BWsgljBLv1OfpN-xLavdM8g6bRuQ9qxmHGSYExn3Om0kLAJjjx0Cm1SnZNGsxhR6mndyF2tseO3fYugzADvwuEX-dHLV65UWqTB6PzJ_b50TpmVZDiTnMVXjAX0eHFQvVmavOkRC4yXE3XmaPcMzWp8X4CJjqjfxecLQ60m0OToRf1TVOhRajTVtInuEyHJu8iPTg”;

note: I have hard coded the token after getting it from first step, which is working fine.

	var settings1 = {
							
			  "async": false,
			  "crossDomain": false,
			  "url": "https://implementation.concursolutions.com/api/image/v1.0/invoice/E84008C1838979851B17B",
			  "method": "GET",
			  "headers": {
				  "Authorization": auth,			    	
			  }
			};

			$.ajax(settings1).done(function (response1) {
			  console.log(response1);
			  url = response1.id;
			  console.alert(response1.id);
			});	

Response is OK.

Question is - Hardcode is working but I need to pass the variable, why it’s not working.
Please help me on this, I have sucked several days here.