SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

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

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
alepage
Barite | Level 11

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.);

 

View solution in original post

5 REPLIES 5
ChrisHemedinger
Community Manager

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.

Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!
alepage
Barite | Level 11

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.);

 

ChrisHemedinger
Community Manager

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.

Register for SAS Innovate 2025!! The premier event for SAS users, May 6-9 in Orlando FL. Sign up now for the best deals!
alepage
Barite | Level 11
I have spent many hours to seach and to develop. I thought that it will be a good idea to share with the community

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2609 views
  • 1 like
  • 2 in conversation