<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how to get sas access token in proc http? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/888106#M350901</link>
    <description>&lt;P&gt;yes, the sas intelligent has the same host as&amp;nbsp; the sas session.&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;</description>
    <pubDate>Mon, 07 Aug 2023 09:32:44 GMT</pubDate>
    <dc:creator>Mayt</dc:creator>
    <dc:date>2023-08-07T09:32:44Z</dc:date>
    <item>
      <title>how to get sas access token in proc http?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/887840#M350761</link>
      <description>&lt;P&gt;Hi guys I've tried to get access token in proc http, but got errors&lt;/P&gt;&lt;P&gt;here are whative tried:&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; got&amp;nbsp;404 Not Found in resp&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;filename resp "myapi.txt";

/* must include content-type/CT= option */
proc http 
 url="https://serviceaddress/oauth/access_token"
 in='grant_type=client_credentials&amp;amp;client_id=XXXXXXXX&amp;amp;client_secret=YYYYYYYY' 
 ct="application/x-www-form-urlencoded"
 out=resp
 method='POST'
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;2. got&amp;nbsp;{"error":"invalid_client","error_description":"Unauthorized grant type"}&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;filename resp "myapi.txt;

proc http url="https://serviceaddress/SASLogon/oauth/token"
	method='post'
	in="grant_type=password%nrstr(&amp;amp;username=)&amp;amp;USERNAME.%nrstr(&amp;amp;password=)&amp;amp;PASSWORD."
	username="&amp;amp;CLIENT_ID."
	password="&amp;amp;CLIENT_SECRET."
	out=resp
	auth_basic verbose;
	debug level=3;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;if I change &amp;amp;client_id.,&amp;amp;client_secret. to other id/secret I'll get&amp;nbsp; unauthorized too, but with bad credentials.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Aug 2023 10:27:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/887840#M350761</guid>
      <dc:creator>Mayt</dc:creator>
      <dc:date>2023-08-04T10:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to get sas access token in proc http?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/887842#M350763</link>
      <description>&lt;P&gt;Is it the same SAS Viya host in which the SAS session is running?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can simply use this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Macro variable containg the Viya Host name;
%let viyaHost = %sysfunc(getoption(SERVICESBASEURL)); 
%let folderName = &amp;lt;string-that-folder-name-must-contain&amp;gt;;

* 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 = "&amp;amp;viyahost./folders/folders"
    out= folders
    oauth_bearer = sas_services
    query = ('limit'='5' 'filter'="contains('name', &amp;amp;folderName.)");
    headers
        'Accept'= 'application/vnd.sas.collection+json';
run;
libname folders json;

proc print data=folders.items noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you call upon another environment or even call from SAS 9.4 then try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/****************************
	AUTHENTICATION TO VIYA
*****************************/
* Macro variables to configure the connection;
%let viyaHost = &amp;lt;Enter-your-SAS-Viya-Host-Base-URL-Here&amp;gt;; 
%let user = &amp;lt;Enter-your-SAS-Viya-username-here&amp;gt;;
%let pw = &amp;lt;Enter-your-SAS-Viya-password-here&amp;gt;;

filename outResp temp;
 
* Get authentication token with the scope of the SAS Viya CLI;
proc http
	url="&amp;amp;viyaHost./SASLogon/oauth/token"
	in="grant_type=password&amp;amp;username=&amp;amp;user.&amp;amp;password=&amp;amp;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 = &amp;lt;Enter-the-folder-of-interest-here&amp;gt;;

filename folders temp;

* Call the folders API;
proc http
    url = "&amp;amp;viyaHost./folders/folders"
    out= folders
    query = ('limit'='5' 'filter'="contains('name', &amp;amp;folderName.)");
    headers 'Accept'= 'application/vnd.sas.collection+json';
	headers 'Authorization' = "Bearer &amp;amp;accessToken.";
run;

libname folders json;

title "First five folders containg the &amp;amp;folderName. in their name";
proc print data=folders.items noobs;
run;
title;

* Clean up;
libname folders clear;
filename folders clear;
%symdel viyaHost accessToken;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Aug 2023 11:38:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/887842#M350763</guid>
      <dc:creator>DavidHD</dc:creator>
      <dc:date>2023-08-04T11:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: how to get sas access token in proc http?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/888106#M350901</link>
      <description>&lt;P&gt;yes, the sas intelligent has the same host as&amp;nbsp; the sas session.&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 09:32:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/888106#M350901</guid>
      <dc:creator>Mayt</dc:creator>
      <dc:date>2023-08-07T09:32:44Z</dc:date>
    </item>
    <item>
      <title>Re: how to get sas access token in proc http?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/888126#M350905</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;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.</description>
      <pubDate>Mon, 07 Aug 2023 13:53:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/888126#M350905</guid>
      <dc:creator>DavidHD</dc:creator>
      <dc:date>2023-08-07T13:53:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to get sas access token in proc http?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/888299#M350977</link>
      <description>&lt;P&gt;Thank you so much!! It works.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 08:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-get-sas-access-token-in-proc-http/m-p/888299#M350977</guid>
      <dc:creator>Mayt</dc:creator>
      <dc:date>2023-08-08T08:13:19Z</dc:date>
    </item>
  </channel>
</rss>

