1
-
1
+ # -*- coding: utf-8 -*-
2
2
3
3
import json
4
4
try :
15
15
16
16
class Credentials (object ):
17
17
18
- _PROPERTIES = ("token" ,
19
- "refresh_token" ,
20
- "token_uri" ,
21
- "account_id" ,
22
- "client_id" ,
23
- "scopes" )
24
-
25
- def __init__ (self , token , refresh_token , token_uri , account_id ,
26
- client_id , scopes , expires_at = None ):
18
+ PROPERTIES = ("token" ,
19
+ "refresh_token" ,
20
+ "token_uri" ,
21
+ "account_id" ,
22
+ "client_id" ,
23
+ "client_secret" ,
24
+ "scopes" )
25
+
26
+ def __init__ (self , client_id = None , client_secret = None , token = None ,
27
+ refresh_token = None , token_uri = None , account_id = None ,
28
+ scopes = None , expires_at = None ):
27
29
self .token = token
28
30
self .refresh_token = refresh_token
29
31
self .token_uri = token_uri
30
32
self .account_id = account_id
31
33
self .client_id = client_id
32
- if isinstance (scopes , (list , tuple )):
33
- self .scopes = ' ' .join (scopes )
34
- else :
35
- self .scopes = scopes
34
+ self .client_secret = client_secret
35
+ self .scopes = scopes
36
36
self .expires_at = expires_at
37
37
38
38
@classmethod
39
- def from_oauth2_flow_result (cls , result ):
39
+ def from_oauth2_flow_result (cls , result , client_id , client_secret ):
40
40
"""Return a :py:class:`Credentials` instance from OAuth2 flow result.
41
41
"""
42
- return cls (result .access_token , result .refresh_token , result .url_state ,
43
- result .account_id , result .user_id , result .scope , result .expires_at )
42
+ return cls (client_id , client_secret , result .access_token , result .refresh_token ,
43
+ result .url_state , result .account_id , result .scope .split (' ' ),
44
+ result .expires_at )
44
45
45
46
@classmethod
46
47
def from_authorized_user_file (cls , filename ):
@@ -58,15 +59,12 @@ def to_json(self, strip=None):
58
59
59
60
:returns: A JSON representation of this instance, suitable to write in file.
60
61
"""
61
- prep = dict ((k , getattr (self , k )) for k in Credentials ._PROPERTIES )
62
+ prep = dict ((k , getattr (self , k )) for k in Credentials .PROPERTIES )
62
63
63
64
# Remove entries that explicitely need to be removed
64
65
if strip is not None :
65
66
prep = {k : v for k , v in prep .items () if k not in strip }
66
67
67
- # Save scopes has list
68
- prep ["scopes" ] = prep ["scopes" ].split (' ' )
69
-
70
68
return json .dumps (prep )
71
69
72
70
@@ -88,9 +86,9 @@ class InstalledAppFlow(object):
88
86
"The authentication flow has completed. You may close this window."
89
87
)
90
88
91
- def __init__ (self , app_key , app_secret , scopes = None , client_type = 'offline' ):
92
- self .app_key = app_key
93
- self .app_secret = app_secret
89
+ def __init__ (self , app_key , app_secret , scopes , client_type = 'offline' ):
90
+ self .client_id = app_key
91
+ self .client_secret = app_secret
94
92
self .client_type = client_type
95
93
self .scopes = scopes
96
94
self .redirect_uri = None
@@ -180,12 +178,13 @@ def run_local_server(self, host="localhost", port=8080,
180
178
self .redirect_uri )
181
179
182
180
session = {}
183
- flow = DropboxOAuth2Flow (self .app_key ,
181
+ flow = DropboxOAuth2Flow (self .client_id ,
184
182
self .redirect_uri ,
185
183
session ,
186
184
"dropbox-auth-csrf-token" ,
187
- self .app_secret ,
188
- token_access_type = self .client_type )
185
+ self .client_secret ,
186
+ token_access_type = self .client_type ,
187
+ scope = self .scopes )
189
188
auth_url = flow .start ()
190
189
191
190
if open_browser :
@@ -203,7 +202,7 @@ def run_local_server(self, host="localhost", port=8080,
203
202
# This closes the socket
204
203
local_server .server_close ()
205
204
206
- return Credentials .from_oauth2_flow_result (result )
205
+ return Credentials .from_oauth2_flow_result (result , self . client_id , self . client_secret )
207
206
208
207
209
208
class _WSGIRequestHandler (wsgiref .simple_server .WSGIRequestHandler ):
0 commit comments