def get_twitter token no-token domain twitter com if token no-token ac

 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
def get_twitter(token='no-token', domain='twitter.com'):
if token == 'no-token':
# access token, stored in database
token = config.access['token']
return twitterapi.Twitter(
settings.CONSUMER_KEY, settings.CONSUMER_SECRET,
token=token, domain=domain)
def oauth_start(request):
tw = utils.get_twitter(None)
token = tw.oauth.request_token()
tw = utils.get_twitter(token)
url = tw.oauth.authorize()
# storing request token in session because we need it to get access token
request.session['unauthed_token'] = token.to_string()
return HttpResponseRedirect(url)
def oauth_finish(request):
token = request.session.get('unauthed_token', None)
if not token:
return HttpResponseBadRequest('No un-authed token cookie')
token = oauth.OAuthToken.from_string(token)
# oauth_token GET parameter is a token key. Not sure this check is necessary
if token.key != request.GET.get('oauth_token'):
return HttpResponse("Something went wrong! Tokens do not match")
tw = utils.get_twitter(token)
# exchanging request token for access token
token = tw.oauth.access_token()
tw = utils.get_twitter(token)
# logged in!
userinfo = tw.account.verify_credentials()
# storing token in a database
config.access = {'login': userinfo['screen_name'],
'token': token.to_string()}
return HttpResponseRedirect(reverse('config_change'))
def oauth_info(request):
# check that everything works
tw = utils.get_twitter()
userinfo = tw.account.verify_credentials()
return HttpResponse(userinfo)