Error: Generating Access Token while calling API from External Application

We have created Widget (Using Velocity) which is dynamically fetching group, forum, tags based on user selection. The widget is working fine within community.

Our requirement is to call this widget from external application.

To implement this we are generating access token using the instruction (Using the Authorization Code Grant Type) provided at the below link (community.telligent.com/.../authentication

We are getting the below error.

'community-qa.thomsonreuters.com/.../authorize from origin 'community-dev.thomsonreuters.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request."

Any help will be appreciated.

Parents Reply
  • Hi Satish,

    Thank you for your valuable reply.

    Now we can get the access_token Using the Implicit Grant Type. But we are facing the same CORS error when calling to access the rest-api with this acess-token.

    #1

    I have attached the code and screenshot for your further reference.

    Code:
    --------

    function getAccessToken(AuthKey){
    	const http = new XMLHttpRequest();
    	var clId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    	var secretId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    	var g_url = "https://community-qa.thomsonreuters.com/api.ashx/v2/groups/12/forums.json";
    	http.open('GET', g_url,true);
    	http.setRequestHeader("Access-Control-Allow-Origin", "*");
    	http.setRequestHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    	
    	http.setRequestHeader('Authorization', 'OAuth '+AuthKey);
    	
    	http.send(null);
    	http.onload = ()=>{
    	  if(http.status===200) {
    		//document.getElementById('demo').innerHTML=http.response;
    	  } else {
    		 console.log(`error ${http.status}`);
    	  }
    	}
    }
    const queryString=window.location.search;
    const urlParams = new URLSearchParams(queryString);
    const codeParam = urlParams.get('access_token');
    if(codeParam===null) {
    	const clId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    	const redirectpage="https://community-dev.thomsonreuters.com/test5.html";
    	const url = 'https://community-qa.thomsonreuters.com/api.ashx/v2/oauth/authorize';
    	
    	const path = url+'?response_type=token&client_id='+clId+'&redirect_uri='+redirectpage;
    	location.replace(path);
    } else if (codeParam!==null && codeParam!=='' && codeParam!== undefined) {
    	getAccessToken(codeParam);
    }

    Screenshot:

    and Kindly let me know, if there is any chance to skip Authorize(allow-deny) xxxx page?(user doesn't need to click Allow)

    #2

    As per the documentation, we have tried another method called Authorization Code Grant Type. By using this method, we have followed two step to get acess-token.

    In first step, we have received the authorization code successfully by following url api.ashx/v2/oauth/authorize?response_type=code&client_id=xxxxxxxxxxx&redirect_uri=xxxxxxx

    In second step, we got the same CORS error when making the request(api.ashx/v2/oauth/token) for getting access-token.

    Below I have attached the code and screenshot for your reference.

    Code:
    --------

    function getAccessToken(AccessToken){
    		const http = new XMLHttpRequest();
    		var clId="xxxxxxxxxxxxxxxxxxxxxxxxx";
    		var secretId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    		var g_url = "https://community-qa.thomsonreuters.com/api.ashx/v2/oauth/token";
    		http.open('POST', g_url,true);
    		http.setRequestHeader("Access-Control-Allow-Origin", "*");
    		http.setRequestHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    		http.setRequestHeader('Content-Type', 'application/json');
    		//http.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    		//http.setRequestHeader('grant_type', 'authorization_code');
    		//http.setRequestHeader('client_id', clId);
    		//http.setRequestHeader('client_secret',secretId);
    		//http.setRequestHeader('code', AccessToken);
    		const data = {
    			'grant_type':'authorization_code',
    			'client_id':clId,
    			'client_secret':secretId,
    			'code':AccessToken
    		}
    		http.send(JSON.stringify(data));
    		http.onload = ()=>{
    		  if(http.status===200) {
    			//document.getElementById('demo').innerHTML=http.response;
    		  } else {
    			 console.log(`error ${http.status}`);
    		  }
    		}
    	}
    	const queryString=window.location.search;
    	const urlParams = new URLSearchParams(queryString);
    	const codeParam = urlParams.get('code');
    	if(codeParam===null) {
    		const clId="xxxxxxxxxxxxxxxxxxxxxxxxxx";
    		const redirectpage="https://community-dev.thomsonreuters.com/Test4.html";
    		const url = 'https://community-qa.thomsonreuters.com/api.ashx/v2/oauth/authorize';
    		const path = url+'?response_type=code&client_id='+clId+'&redirect_uri='+redirectpage;
    		location.replace(path);
    	} else if (codeParam!==null && codeParam!=='' && codeParam!== undefined) {
    		getAccessToken(codeParam);
    	}

    Screenshot

Children