BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Mayt
Obsidian | Level 7

Hi guys I've tried to get access token in proc http, but got errors

here are whative tried:

1.  got 404 Not Found in resp

filename resp "myapi.txt";

/* must include content-type/CT= option */
proc http 
 url="https://serviceaddress/oauth/access_token"
 in='grant_type=client_credentials&client_id=XXXXXXXX&client_secret=YYYYYYYY' 
 ct="application/x-www-form-urlencoded"
 out=resp
 method='POST'
;
run;

2. got {"error":"invalid_client","error_description":"Unauthorized grant type"}

filename resp "myapi.txt;

proc http url="https://serviceaddress/SASLogon/oauth/token"
	method='post'
	in="grant_type=password%nrstr(&username=)&USERNAME.%nrstr(&password=)&PASSWORD."
	username="&CLIENT_ID."
	password="&CLIENT_SECRET."
	out=resp
	auth_basic verbose;
	debug level=3;
run;

if I change &client_id.,&client_secret. to other id/secret I'll get  unauthorized too, but with bad credentials.

1 ACCEPTED SOLUTION

Accepted Solutions
DavidHD
SAS Employee

Is it the same SAS Viya host in which the SAS session is running?

 

If you can simply use this:

* Macro variable containg the Viya Host name;
%let viyaHost = %sysfunc(getoption(SERVICESBASEURL)); 
%let folderName = <string-that-folder-name-must-contain>;

* The authentication is provided by SAS Studio - so there is no need for any of the authentication steps here;
filename folders temp;
proc http
    url = "&viyahost./folders/folders"
    out= folders
    oauth_bearer = sas_services
    query = ('limit'='5' 'filter'="contains('name', &folderName.)");
    headers
        'Accept'= 'application/vnd.sas.collection+json';
run;
libname folders json;

proc print data=folders.items noobs;
run;

If you call upon another environment or even call from SAS 9.4 then try this:

/****************************
	AUTHENTICATION TO VIYA
*****************************/
* Macro variables to configure the connection;
%let viyaHost = <Enter-your-SAS-Viya-Host-Base-URL-Here>; 
%let user = <Enter-your-SAS-Viya-username-here>;
%let pw = <Enter-your-SAS-Viya-password-here>;

filename outResp temp;
 
* Get authentication token with the scope of the SAS Viya CLI;
proc http
	url="&viyaHost./SASLogon/oauth/token"
	in="grant_type=password&username=&user.&password=&pw."
	out=outResp;
	headers 'Content-Type' = ' application/x-www-form-urlencoded';
	headers 'Authorization' = 'Basic c2FzLmNsaTo=';
run;

/*
* Explanation of the header 'Authorization' = 'Basic c2FzLmNsaTo=';
* This is the base64 encoded string sas.cli: which is used as the client scope;
data _null_;
	baseString = 'sas.cli:';
	encodedString = put(baseString, $base64x64.);
	put encodedString=;
run;
*/

libname outResp json;

* Write the access token to a macro variable named accessToken;
proc sql;
	select value into :accessToken
		from outresp.alldata
			where p1 = 'access_token';
quit;

* Clean up;
libname outResp clear;
filename outResp clear;
%symdel user pw;
/****************************
	WORK WITH VIYA APIs
*****************************/
* Parameters for the subsquent API calls;
%let folderName = <Enter-the-folder-of-interest-here>;

filename folders temp;

* Call the folders API;
proc http
    url = "&viyaHost./folders/folders"
    out= folders
    query = ('limit'='5' 'filter'="contains('name', &folderName.)");
    headers 'Accept'= 'application/vnd.sas.collection+json';
	headers 'Authorization' = "Bearer &accessToken.";
run;

libname folders json;

title "First five folders containg the &folderName. in their name";
proc print data=folders.items noobs;
run;
title;

* Clean up;
libname folders clear;
filename folders clear;
%symdel viyaHost accessToken;

View solution in original post

4 REPLIES 4
DavidHD
SAS Employee

Is it the same SAS Viya host in which the SAS session is running?

 

If you can simply use this:

* Macro variable containg the Viya Host name;
%let viyaHost = %sysfunc(getoption(SERVICESBASEURL)); 
%let folderName = <string-that-folder-name-must-contain>;

* The authentication is provided by SAS Studio - so there is no need for any of the authentication steps here;
filename folders temp;
proc http
    url = "&viyahost./folders/folders"
    out= folders
    oauth_bearer = sas_services
    query = ('limit'='5' 'filter'="contains('name', &folderName.)");
    headers
        'Accept'= 'application/vnd.sas.collection+json';
run;
libname folders json;

proc print data=folders.items noobs;
run;

If you call upon another environment or even call from SAS 9.4 then try this:

/****************************
	AUTHENTICATION TO VIYA
*****************************/
* Macro variables to configure the connection;
%let viyaHost = <Enter-your-SAS-Viya-Host-Base-URL-Here>; 
%let user = <Enter-your-SAS-Viya-username-here>;
%let pw = <Enter-your-SAS-Viya-password-here>;

filename outResp temp;
 
* Get authentication token with the scope of the SAS Viya CLI;
proc http
	url="&viyaHost./SASLogon/oauth/token"
	in="grant_type=password&username=&user.&password=&pw."
	out=outResp;
	headers 'Content-Type' = ' application/x-www-form-urlencoded';
	headers 'Authorization' = 'Basic c2FzLmNsaTo=';
run;

/*
* Explanation of the header 'Authorization' = 'Basic c2FzLmNsaTo=';
* This is the base64 encoded string sas.cli: which is used as the client scope;
data _null_;
	baseString = 'sas.cli:';
	encodedString = put(baseString, $base64x64.);
	put encodedString=;
run;
*/

libname outResp json;

* Write the access token to a macro variable named accessToken;
proc sql;
	select value into :accessToken
		from outresp.alldata
			where p1 = 'access_token';
quit;

* Clean up;
libname outResp clear;
filename outResp clear;
%symdel user pw;
/****************************
	WORK WITH VIYA APIs
*****************************/
* Parameters for the subsquent API calls;
%let folderName = <Enter-the-folder-of-interest-here>;

filename folders temp;

* Call the folders API;
proc http
    url = "&viyaHost./folders/folders"
    out= folders
    query = ('limit'='5' 'filter'="contains('name', &folderName.)");
    headers 'Accept'= 'application/vnd.sas.collection+json';
	headers 'Authorization' = "Bearer &accessToken.";
run;

libname folders json;

title "First five folders containg the &folderName. in their name";
proc print data=folders.items noobs;
run;
title;

* Clean up;
libname folders clear;
filename folders clear;
%symdel viyaHost accessToken;
Mayt
Obsidian | Level 7

yes, the sas intelligent has the same host as  the sas session.

well, I want to try your first code, but I'm little confused what the variable folderName is. Is it the folder that contains sas token?

DavidHD
SAS Employee
the folderName macro variable was just there as my example features the /folders/folders API endpoint and it filters for folders that have the value of that macro variable.

The SAS token is implicitly taken care of through the proc http statement oauth_bearer = sas_services - this is comfort feature that reuses your SAS sessions token to authenticate with SAS Viya.
Mayt
Obsidian | Level 7

Thank you so much!! It works.

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1055 views
  • 1 like
  • 2 in conversation