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
44  @source https://github.com/sasjs/core
45 
46  <h4> Dependencies </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;
66 options 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(iftrue=(&grant_type=password and (%str(&user)=%str() or %str(&pass)=%str()))
77  ,mac=&sysmacroname
78  ,msg=%str(username / password required)
79 )
80 
81 %if %mf_existds(&inds) %then %do;
82  data _null_;
83  set &inds;
84  call symputx('client_id',client_id,'l');
85  call symputx('client_secret',client_secret,'l');
86  call symputx("&refresh_token_var",&refresh_token_var,'l');
87  run;
88 %end;
89 
90 %mp_abort(iftrue=(%str(&client_id)=%str() or %str(&client_secret)=%str())
91  ,mac=&sysmacroname
92  ,msg=%str(client / secret must both be provided)
93 )
94 
95 /**
96  * Request access token
97  */
98 %local base_uri; /* location of rest apis */
99 %let base_uri=%mf_getplatform(VIYARESTAPI);
100 
101 %let fref1=%mf_getuniquefileref();
102 proc http method='POST'
103  in="grant_type=refresh_token%nrstr(&)refresh_token=&&&refresh_token_var"
104  out=&fref1
105  url="&base_uri/SASLogon/oauth/token"
106  WEBUSERNAME="&client_id"
107  WEBPASSWORD="&client_secret"
108  AUTH_BASIC;
109  headers "Accept"="application/json"
110  "Content-Type"="application/x-www-form-urlencoded";
111 run;
112 /*data _null_;infile &fref1;input;put _infile_;run;*/
113 
114 /**
115  * Extract access / refresh tokens
116  */
117 
118 %let libref=%mf_getuniquelibref();
119 libname &libref JSON fileref=&fref1;
120 
121 /* extract the token */
122 data &outds;
123  set &libref..root;
124  call symputx("&access_token_var",access_token);
125  call symputx("&refresh_token_var",refresh_token);
126 run;
127 
128 
129 libname &libref clear;
130 filename &fref1 clear;
131 
132 %mend;