- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
Up to now, I was using x-api-token for the get or post method.
Now I need to use a bearer token .
Does someone got a sample code I can use to obtain a bearer token
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
After several tries, I have found the solution, I was looking for. I am sharing it with the community.
The task is to obtain via an API call, a Bearer Token but by providing the Basic Authentication.
So, the first step is to convert the client_id and the client_secret using a base64 encoder to obtain a base64-encoded string, i.e., the Basic Authentication value (&Basic_Auth, in the SAS code below)
Thereafter, a proc http call is done, providing the above-mentioned Basic Authentication value, to the appropriate web site, to obtain a Bearer Token. This Bearer Token is replacing the use of an API Token and the difference is that this Bearer Token is valid only for a certain period.
%macro BearerToken(client_id, client_secret);/* Example of call: %BearerToken(&client_id., &client_secret.); */
/* Reading the input values */
%put reading the input values: &=client_id &=client_secret;
/**** Converting the Client_id and the client_secret using base64 encoder to obtain the corresponding Basic Authentication value ***/
data _null_;
length newString $200.;
newString=cats("&client_id.",":","&client_secret.");
call symputx('Basic_Auth',put(trim(newString),$base64x300.));
run;
%put &=Basic_Auth;
/* Sending the Basic Authentication value to the appropriate web site, via an API call to obtain the Bearer_token ****/
options set=SSLREQCERT="allow";
filename resp temp;
/* must include content-type/CT= option */
proc http
url="https://ca1.qualtrics.com/oauth2/token"
method='POST'
AUTH_BASIC
AUTH_NEGOTIATE
ct="application/x-www-form-urlencoded"
in='grant_type=client_credentials&scope=read:survey_responses'
out=resp;
headers "Authorization" = "Basic &Basic_Auth.";
;
run;
/* Checking the log for the API Call */
%put &=SYS_PROCHTTP_STATUS_CODE;
data _null_;
rc=jsonpp('resp','log');
run;
/* Getting the Bearer Token */
libname auth json fileref=resp;
data _null_;
set auth.root;
call symputx('BearerToken',access_token,'g');
run;
%put &=BearerToken;
%mend BearerToken;
%BearerToken(&client_id., &client_secret.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Which API service? Can you share?
OAuth 2 (usually what is in place for bearer token) often requires an interactive step to get the initial auth code, then token can be refreshed/used via PROC HTTP.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello Chris,
Here's the information: https://api.qualtrics.com/9398592961ced-o-auth-authentication-client-credentials
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
After several tries, I have found the solution, I was looking for. I am sharing it with the community.
The task is to obtain via an API call, a Bearer Token but by providing the Basic Authentication.
So, the first step is to convert the client_id and the client_secret using a base64 encoder to obtain a base64-encoded string, i.e., the Basic Authentication value (&Basic_Auth, in the SAS code below)
Thereafter, a proc http call is done, providing the above-mentioned Basic Authentication value, to the appropriate web site, to obtain a Bearer Token. This Bearer Token is replacing the use of an API Token and the difference is that this Bearer Token is valid only for a certain period.
%macro BearerToken(client_id, client_secret);/* Example of call: %BearerToken(&client_id., &client_secret.); */
/* Reading the input values */
%put reading the input values: &=client_id &=client_secret;
/**** Converting the Client_id and the client_secret using base64 encoder to obtain the corresponding Basic Authentication value ***/
data _null_;
length newString $200.;
newString=cats("&client_id.",":","&client_secret.");
call symputx('Basic_Auth',put(trim(newString),$base64x300.));
run;
%put &=Basic_Auth;
/* Sending the Basic Authentication value to the appropriate web site, via an API call to obtain the Bearer_token ****/
options set=SSLREQCERT="allow";
filename resp temp;
/* must include content-type/CT= option */
proc http
url="https://ca1.qualtrics.com/oauth2/token"
method='POST'
AUTH_BASIC
AUTH_NEGOTIATE
ct="application/x-www-form-urlencoded"
in='grant_type=client_credentials&scope=read:survey_responses'
out=resp;
headers "Authorization" = "Basic &Basic_Auth.";
;
run;
/* Checking the log for the API Call */
%put &=SYS_PROCHTTP_STATUS_CODE;
data _null_;
rc=jsonpp('resp','log');
run;
/* Getting the Bearer Token */
libname auth json fileref=resp;
data _null_;
set auth.root;
call symputx('BearerToken',access_token,'g');
run;
%put &=BearerToken;
%mend BearerToken;
%BearerToken(&client_id., &client_secret.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for sharing your solution related to this specific provider, Qualtrics. We have many SAS customers who also use Qualtrics (including us), so the technique is helpful. Readers should keep in mind that different APIs use different authentication schemes, and that OAuth -- while a conceptual standard -- can vary in its implementation and nuances for different APIs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content