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.
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;
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;
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?
Thank you so much!! It works.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.