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