<?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 use PROC HTTP with OAUTH to get an access token from a remote server in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778733#M247918</link>
    <description>&lt;P&gt;I&amp;nbsp;&lt;STRONG&gt;think&lt;/STRONG&gt; this is the PROC HTTP equivalent of that command.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;filename temp out;
filename temp hdrs;

%let data = %sysfunc(urlencode(grant_type=client_credentials%str(&amp;amp;)client_id=123456.XXXXXXXX%str(&amp;amp;)client_secret=YYYYYYYYYYY));
proc http
 url="https://serviceaddress/oauth/access_token" method='POST'
 out=out
 in="&amp;amp;data."
 ct="application/x-www-form-urlencoded"
 headerout=hdrs;
run;

/* report HTTP response code and content */
%put &amp;amp;SYS_PROCHTTP_STATUS_CODE.;
data _null_;
 rc = jsonpp('out','log');
run;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some REST API services are very picky -- you need to specify the content-type (CT= option here) and the data must be URL-encoded just so...even when it doesn't appear on the URL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Nov 2021 12:52:38 GMT</pubDate>
    <dc:creator>ChrisHemedinger</dc:creator>
    <dc:date>2021-11-05T12:52:38Z</dc:date>
    <item>
      <title>How to use PROC HTTP with OAUTH to get an access token from a remote server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778490#M247799</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have skimmed through most articles on PROC HTTP and have been trying to get this work for days now but without success.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS 9.4 M7.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need access token from a server where I have username and password. When I use Postman for this, it works without any problems:&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.dropbox.com/s/2jv7wdy4tcqzrkc/Postman.jpg?dl=0" target="_blank" rel="noopener"&gt;https://www.dropbox.com/s/2jv7wdy4tcqzrkc/Postman.jpg?dl=0&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It should be a simple matter of transferring the commands from Postman into SAS but nothing works for me. I must be missing something simple in the PROC HTTP syntax.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example of what I have tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="code 1.jpg" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65409i893EFD079DC27C56/image-size/large?v=v2&amp;amp;px=999" role="button" title="code 1.jpg" alt="code 1.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="code 2.jpg" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65410iF76216CDB799331B/image-size/large?v=v2&amp;amp;px=999" role="button" title="code 2.jpg" alt="code 2.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here is a spec for the API:&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.dropbox.com/s/l36f1ij2gx5j2iv/API%20spec.jpg?dl=0" target="_blank" rel="noopener"&gt;https://www.dropbox.com/s/l36f1ij2gx5j2iv/API%20spec.jpg?dl=0&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will be extremely grateful for any help with this issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 14:04:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778490#M247799</guid>
      <dc:creator>Hessner</dc:creator>
      <dc:date>2021-11-04T14:04:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to use PROC HTTP with OAUTH to get an access token from a remote server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778520#M247821</link>
      <description>&lt;P&gt;I do this all of the time from a variety of services. One difference from your Postman screenshot and your SAS code is that you are putting data on the URL (SAS) vs in the body (Postman). This is okay, but you want to make sure you're comparing the two equally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc http
 url="https://serviceaddress/oauth/access_token" method='POST'
 out=out
 in="grant_type=client_credentials&amp;amp;client_id=XXXXX&amp;amp;client_secret=XXXXX&amp;amp;scope=SCOPES";
 headerout=hdrs;
run;&lt;/LI-CODE&gt;
&lt;P&gt;If you take your working example in Postman and view its code (cURL version) and reply with that here (redacting any secrets), then we can help more.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 16:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778520#M247821</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2021-11-04T16:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to use PROC HTTP with OAUTH to get an access token from a remote server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778562#M247837</link>
      <description>&lt;P&gt;Hi Chris,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your amazingly swift reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried your code. I tried both, with and without SCOPE at the end. It feels like it's almost about to work but I am still getting 400 Bad request.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With client_id=123456.XXXXXXXX and client_secret=YYYYYYYYYYY, this is what gets stored in the 'Pout' file:&lt;/P&gt;&lt;P&gt;{"code":101,"title":"UnsupportedGrantType","details":"Grant type not supported: client_credentials123456.XXXXXXXX=123456.XXXXXXXXYYYYYYYYYYY=YYYYYYYYY"}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the cURL code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;STRONG&gt;curl --location --request POST&lt;/STRONG&gt; 'https:/serviceaddress/oauth/access_token' \&lt;BR /&gt;&lt;STRONG&gt;--header&lt;/STRONG&gt; 'Content-Type: application/x-www-form-urlencoded' \&lt;BR /&gt;&lt;STRONG&gt;--header&lt;/STRONG&gt; 'Cookie: AWSALB=yvNLDYnADjC5+nHmYMiMNGIXOs6GxO0F+YWXSCXlP/judzxJpCNUxWp/2TvIfwGR14xZo+9h/If1zzlh9NHuZWnVaTojJo80UcBFWjbf0TRn1e7P/aOdq17n+OsD; AWSALBCORS=yvNLDYnADjC5+nHmYMiMNGIXOs6GxO0F+YWXSCXlP/judzxJpCNUxWp/2TvIfwGR14xZo+9h/If1zzlh9NHuZWnVaTojJo80UcBFWjbf0TRn1e7P/aOdq17n+OsD; PHPSESSID=b2bed2biifr3s5aij6b9hvbp03; csrftoken=fdd07e60e176fac6731cd81746eac14f3613' \&lt;BR /&gt;&lt;STRONG&gt;--data-urlencode&lt;/STRONG&gt; 'grant_type=client_credentials' \&lt;BR /&gt;&lt;STRONG&gt;--data-urlencode&lt;/STRONG&gt; 'client_id=123456.XXXXXXXX' \&lt;BR /&gt;&lt;STRONG&gt;--data-urlencode&lt;/STRONG&gt; 'client_secret=YYYYYYYYYYY'&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help again, much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 17:45:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778562#M247837</guid>
      <dc:creator>Hessner</dc:creator>
      <dc:date>2021-11-04T17:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to use PROC HTTP with OAUTH to get an access token from a remote server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778733#M247918</link>
      <description>&lt;P&gt;I&amp;nbsp;&lt;STRONG&gt;think&lt;/STRONG&gt; this is the PROC HTTP equivalent of that command.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;filename temp out;
filename temp hdrs;

%let data = %sysfunc(urlencode(grant_type=client_credentials%str(&amp;amp;)client_id=123456.XXXXXXXX%str(&amp;amp;)client_secret=YYYYYYYYYYY));
proc http
 url="https://serviceaddress/oauth/access_token" method='POST'
 out=out
 in="&amp;amp;data."
 ct="application/x-www-form-urlencoded"
 headerout=hdrs;
run;

/* report HTTP response code and content */
%put &amp;amp;SYS_PROCHTTP_STATUS_CODE.;
data _null_;
 rc = jsonpp('out','log');
run;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some REST API services are very picky -- you need to specify the content-type (CT= option here) and the data must be URL-encoded just so...even when it doesn't appear on the URL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Nov 2021 12:52:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/778733#M247918</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2021-11-05T12:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to use PROC HTTP with OAUTH to get an access token from a remote server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/779567#M248306</link>
      <description>&lt;P class=""&gt;&lt;SPAN&gt;Here is the final, elegant solution that Chris did for me. It works like a charm.&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;filename resp temp;

/* 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;

%put &amp;amp;=SYS_PROCHTTP_STATUS_CODE;
data _null_;
 rc=jsonpp('resp','log');
run;

libname auth json fileref=resp;
data _null_;
 set auth.root;
 call symputx('token',access_token);
run;

%put &amp;amp;=token;

filename bldgs temp;
proc http 
 /* increase the number of records coming back to 1000*/
 url="https://serviceaddress/api/buildings?number=1000"
 method='GET'
 ct='application/json'
 out=bldgs;
 headers "Authorization" = "Bearer &amp;amp;token.";
 ;
run;
%put &amp;amp;=SYS_PROCHTTP_STATUS_CODE;
 
libname bldg json fileref=bldgs;

data bldgs;
 set bldg.data;
 finYear = input(financialYearStart,yymmdd10.);
 format finYear date9.;
run;

proc means data=bldgs;
 var managementFee;
 class finYear;
 format finYear year.;
run;&lt;/LI-CODE&gt;
&lt;P class=""&gt;This API implementation has been quite unforgiving, meaning that everything in the code had to be just right for it to work. Moreover, the error messages were not helpful, it would send the generic 500 server error for every wrong attempt.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks for your help Chris!&lt;/P&gt;</description>
      <pubDate>Wed, 10 Nov 2021 14:40:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-PROC-HTTP-with-OAUTH-to-get-an-access-token-from-a/m-p/779567#M248306</guid>
      <dc:creator>Hessner</dc:creator>
      <dc:date>2021-11-10T14:40:39Z</dc:date>
    </item>
  </channel>
</rss>

