Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Automate token retrieval from Rest API for sales force

Hello,

 

In order to access data from Sales force, I do the following steps:

1) I use postman to get access token using post method

2) After getting the token, I enter the token data in the res connector api to extract data from sales force.

 

 

The problem with the above approach is, the token is only valid for 30 mins. So every time I need to access data, I have to again repeat the above steps.

 

I want to automate this process. 

Please advice

Labels (2)
1 Reply
stevedark
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi @Anonymous 

The process that you need to follow here is to do a POST to the first API end point that will give you the token. You then need to PEEK that into a variable, which you can then inject into the second API request, by using WITH CONNECTION.

I've not got code for SalesForce, but this code for connecting to the Twitter API should give you an idea of the workflow.

if vBearerKey = '' or IsNull(vBearerKey) or vBearerLastGot < (vNow - vBearerRefreshDays) then
	TRACE Getting New Bearer Key;
    
    LIB CONNECT TO '$(v_POST_Connector)';
    
    tmpBearerKey:
    SQL SELECT
	"tokem_type",
        "access_token"
    FROM JSON (wrap on) "root" PK "__KEY_root"
	WITH CONNECTION (  
      URL "https://api.twitter.com/oauth2/token",
      HTTPHEADER "content-type" "application/x-www-form-urlencoded",
      HTTPHEADER "Authorization" "Basic $(vBASE64Key)",
      QUERY "grant_type" "client_credentials"
    )
	;

	let vBearerKey = peek('access_token', -1, 'tmpBearerKey');

	DROP TABLE tmpBearerKey;

	let vBearerLastGot = now();
	let vLastBearerKey = vBearerKey;
end if


let vMaxTweetURL = if(vMinTweet = 0, '', '&max_id=$(vMinTweet)');

RestConnectorMasterTable:
SQL SELECT 
	[... removed for brevity ...]
FROM JSON (wrap on) "root" PK "__KEY_root"
WITH CONNECTION (  
      URL "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=$(vTwitterName)&trim_user=true&count=200$(vMaxTweetURL)",    
      HTTPHEADER "Authorization" "Bearer $(vBearerKey)"
    )
;

 

Good luck!

Steve