Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mv_tokenrefresh.sas
Go to the documentation of this file.
1/**
2 @file mv_tokenrefresh.sas
3 @brief Get an additional access token using a refresh token
4 @details Before an access token can be obtained, a refresh token is required
5 For that, check out the `mv_tokenauth` macro.
6
7 Usage:
8
9 * prep work - register client, get refresh token, save it for later use ;
10 %mv_registerclient(outds=client)
11 %mv_tokenauth(inds=client,code=wKDZYTEPK6)
12 data _null_;
13 file "~/refresh.token";
14 put "&refresh_token";
15 run;
16
17 * now do the things n stuff;
18 data _null_;
19 infile "~/refresh.token";
20 input;
21 call symputx('refresh_token',_infile_);
22 run;
23 %mv_tokenrefresh(client_id=&client
24 ,client_secret=&secret
25 )
26
27 A great article for explaining all these steps is available here:
28
29 https://blogs.sas.com/content/sgf/2019/01/25/authentication-to-sas-viya/
30
31 @param inds= A dataset containing client_id and client_secret
32 @param outds= A dataset containing access_token and refresh_token
33 @param client_id= The client name (alternative to inds)
34 @param client_secret= client secret (alternative to inds)
35 @param grant_type= valid values are "password" or "authorization_code"
36 (unquoted). The default is authorization_code.
37 @param user= If grant_type=password then provide the username here
38 @param pass= If grant_type=password then provide the password here
39 @param access_token_var= The global macro variable to contain the access token
40 @param refresh_token_var= The global macro variable containing the refresh
41 token
42
43 @version VIYA V.03.04
44 @author Allan Bowe, source: https://github.com/sasjs/core
45
46 <h4> SAS Macros </h4>
47 @li mp_abort.sas
48 @li mf_getplatform.sas
49 @li mf_getuniquefileref.sas
50 @li mf_getuniquelibref.sas
51 @li mf_existds.sas
52
53**/
54
55%macro mv_tokenrefresh(inds=mv_registerclient
56 ,outds=mv_tokenrefresh
57 ,client_id=someclient
58 ,client_secret=somesecret
59 ,grant_type=authorization_code
60 ,user=
61 ,pass=
62 ,access_token_var=ACCESS_TOKEN
63 ,refresh_token_var=REFRESH_TOKEN
64 );
65%global &access_token_var &refresh_token_var;
66options noquotelenmax;
67
68%local fref1 libref;
69
70/* test the validity of inputs */
71%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password)
72 ,mac=&sysmacroname
73 ,msg=%str(Invalid value for grant_type: &grant_type)
74)
75
76%mp_abort(
77 iftrue=(&grant_type=password and (%str(&user)=%str() or %str(&pass)=%str()))
78 ,mac=&sysmacroname
79 ,msg=%str(username / password required)
80)
81
82%if %mf_existds(&inds) %then %do;
83 data _null_;
84 set &inds;
85 call symputx('client_id',client_id,'l');
86 call symputx('client_secret',client_secret,'l');
87 call symputx("&refresh_token_var",&refresh_token_var,'l');
88 run;
89%end;
90
91%mp_abort(iftrue=(%str(&client_id)=%str() or %str(&client_secret)=%str())
92 ,mac=&sysmacroname
93 ,msg=%str(client / secret must both be provided)
94)
95
96/**
97 * Request access token
98 */
99%local base_uri; /* location of rest apis */
100%let base_uri=%mf_getplatform(VIYARESTAPI);
101
102%let fref1=%mf_getuniquefileref();
103proc http method='POST'
104 in="grant_type=refresh_token%nrstr(&)refresh_token=&&&refresh_token_var"
105 out=&fref1
106 url="&base_uri/SASLogon/oauth/token"
107 WEBUSERNAME="&client_id"
108 WEBPASSWORD="&client_secret"
109 AUTH_BASIC;
110 headers "Accept"="application/json"
111 "Content-Type"="application/x-www-form-urlencoded";
112run;
113/*data _null_;infile &fref1;input;put _infile_;run;*/
114
115/**
116 * Extract access / refresh tokens
117 */
118
119%let libref=%mf_getuniquelibref();
120libname &libref JSON fileref=&fref1;
121
122/* extract the token */
123data &outds;
124 set &libref..root;
125 call symputx("&access_token_var",access_token);
126 call symputx("&refresh_token_var",refresh_token);
127run;
128
129
130libname &libref clear;
131filename &fref1 clear;
132
133%mend mv_tokenrefresh;