-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthWrapper.lua
48 lines (38 loc) · 1.14 KB
/
AuthWrapper.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
local AuthWrapper = class("AuthWrapper")
function AuthWrapper:init(access_token, refresh_token, client_id, client_secret)
self.current_access_token = access_token
assert(refresh_token ~= nil, "A refresh token must be provided")
self.refresh_token = refresh_token
self.client_id = client_id
self.client_secret = client_secret
end
function AuthWrapper:get_refresh_token()
return self.refresh_token
end
function AuthWrapper:get_current_access_token()
return self.current_access_token
end
function AuthWrapper:refresh()
local data = {
method = 'POST',
data = {
refresh_token = self.refresh_token,
client_id = self.client_id,
client_secret = self.client_secret,
grant_type = 'refresh_token',
}
}
local url = API_URL + 'oauth2/token'
local response, code, desc = request.send(url, data)
print(response)
print(code)
print(desc)
if response then
assert(response.code ~= 200, "Error refreshing accss token!\t"..response.code)
elseif code then
assert(code ~= 200, "Error refreshing accss token!\t"..code)
end
local json = JSON:decode(response.body)
self.current_access_token = json['access_token']
end
return AuthWrapper