Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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 [in] inds= A dataset containing client_id and client_secret
32 @param [in] outds= A dataset containing access_token and refresh_token
33 @param [in] client_id= The client name (alternative to inds)
34 @param [in] client_secret= client secret (alternative to inds)
35 @param [in] grant_type= valid values are "password" or "authorization_code"
36 (unquoted). The default is authorization_code.
37 @param [in] user= If grant_type=password then provide the username here
38 @param [in] pass= If grant_type=password then provide the password here
39 @param [in] access_token_var= (ACCESS_TOKEN)
40 The global macro variable to contain the access token
41 @param [in] refresh_token_var= (REFRESH_TOKEN)
42 The global macro variable containing the refresh token
43
44 @version VIYA V.03.04
45 @author Allan Bowe, source: https://github.com/sasjs/core
46
47 <h4> SAS Macros </h4>
48 @li mp_abort.sas
49 @li mf_getplatform.sas
50 @li mf_getuniquefileref.sas
51 @li mf_getuniquelibref.sas
52 @li mf_existds.sas
53
54**/
55
56%macro mv_tokenrefresh(inds=mv_registerclient
57 ,outds=mv_tokenrefresh
58 ,client_id=someclient
59 ,client_secret=somesecret
60 ,grant_type=authorization_code
61 ,user=
62 ,pass=
63 ,access_token_var=ACCESS_TOKEN
64 ,refresh_token_var=REFRESH_TOKEN
65 );
66%global &access_token_var &refresh_token_var;
67options noquotelenmax;
68
69%local fref1 libref;
70
71/* test the validity of inputs */
72%mp_abort(iftrue=(&grant_type ne authorization_code and &grant_type ne password)
73 ,mac=&sysmacroname
74 ,msg=%str(Invalid value for grant_type: &grant_type)
75)
76
77%mp_abort(
78 iftrue=(&grant_type=password and (%str(&user)=%str() or %str(&pass)=%str()))
79 ,mac=&sysmacroname
80 ,msg=%str(username / password required)
81)
82
83%if %mf_existds(&inds) %then %do;
84 data _null_;
85 set &inds;
86 call symputx('client_id',client_id,'l');
87 call symputx('client_secret',client_secret,'l');
88 call symputx("&refresh_token_var",&refresh_token_var,'l');
89 run;
90%end;
91
92%mp_abort(iftrue=(%str(&client_id)=%str() or %str(&client_secret)=%str())
93 ,mac=&sysmacroname
94 ,msg=%str(client / secret must both be provided)
95)
96
97/**
98 * Request access token
99 */
100%local base_uri; /* location of rest apis */
101%let base_uri=%mf_getplatform(VIYARESTAPI);
102
103%let fref1=%mf_getuniquefileref();
104proc http method='POST'
105 in="grant_type=refresh_token%nrstr(&)refresh_token=&&&refresh_token_var"
106 out=&fref1
107 url="&base_uri/SASLogon/oauth/token"
108 WEBUSERNAME="&client_id"
109 WEBPASSWORD="&client_secret"
110 AUTH_BASIC;
111 headers "Accept"="application/json"
112 "Content-Type"="application/x-www-form-urlencoded";
113run;
114/*data _null_;infile &fref1;input;put _infile_;run;*/
115
116/**
117 * Extract access / refresh tokens
118 */
119
120%let libref=%mf_getuniquelibref();
121libname &libref JSON fileref=&fref1;
122
123/* extract the token */
124data &outds;
125 set &libref..root;
126 call symputx("&access_token_var",access_token);
127 call symputx("&refresh_token_var",refresh_token);
128run;
129
130
131libname &libref clear;
132filename &fref1 clear;
133
134%mend mv_tokenrefresh;