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
49
50
51
52
|
import sys
import time
from launchpadlib.credentials import RequestTokenAuthorizationEngine
from lazr.restfulclient.errors import HTTPError
ACCESS_TOKEN_POLL_TIME = 1
WAITING_FOR_USER = """Open this link:
{}
to authorize this program to access Launchpad on your behalf.
Waiting to hear from Launchpad about your decision. . . ."""
class AuthorizeRequestTokenWithConsole(RequestTokenAuthorizationEngine):
"""Authorize a token in a server environment (with no browser).
Print a link for the user to copy-and-paste into his/her browser
for authentication.
"""
def __init__(self, *args, **kwargs):
# as implemented in AuthorizeRequestTokenWithBrowser
kwargs['consumer_name'] = None
kwargs.pop('allow_access_levels', None)
super(AuthorizeRequestTokenWithConsole, self).__init__(*args, **kwargs)
def make_end_user_authorize_token(self, credentials, request_token):
"""Ask the end-user to authorize the token in their browser.
"""
authorization_url = self.authorization_url(request_token)
print WAITING_FOR_USER.format(authorization_url)
# if we don't flush we may not see the message
sys.stdout.flush()
while credentials.access_token is None:
time.sleep(ACCESS_TOKEN_POLL_TIME)
try:
credentials.exchange_request_token_for_access_token(
self.web_root)
break
except HTTPError, e:
if e.response.status == 403:
# The user decided not to authorize this
# application.
raise e
elif e.response.status == 401:
# The user has not made a decision yet.
pass
else:
# There was an error accessing the server.
raise e
|