forked from rcrowley/django-twitterauth
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
85 lines (64 loc) · 2.55 KB
/
utils.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Taken almost verbatim from Henrik Lied's django-twitter-oauth app
# http://github.com/henriklied/django-twitter-oauth
import httplib
from django.conf import settings
from django.utils import simplejson as json
from oauth import oauth
signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
TWITTERAUTH_KEY = getattr(settings, 'TWITTERAUTH_KEY', 'OH HAI')
TWITTERAUTH_SECRET = getattr(settings, 'TWITTERAUTH_SECRET', 'OH NOES')
def consumer():
try: return consumer._consumer
except AttributeError:
consumer._consumer = oauth.OAuthConsumer(TWITTERAUTH_KEY, TWITTERAUTH_SECRET)
return consumer._consumer
def connection():
try: return connection._connection
except AttributeError:
connection._connection = httplib.HTTPSConnection('twitter.com')
return connection._connection
def oauth_request(
url,
token,
parameters=None,
signature_method=signature_method,
http_method='GET'
):
req = oauth.OAuthRequest.from_consumer_and_token(
consumer(), token=token, http_url=url,
parameters=parameters, http_method=http_method
)
req.sign_request(signature_method, consumer(), token)
return req
def oauth_response(req):
connection().request(req.http_method, req.to_url())
return connection().getresponse().read()
def get_unauthorized_token(signature_method=signature_method):
req = oauth.OAuthRequest.from_consumer_and_token(
consumer(), http_url='https://twitter.com/oauth/request_token'
)
req.sign_request(signature_method, consumer(), None)
return oauth.OAuthToken.from_string(oauth_response(req))
def get_authorization_url(token, signature_method=signature_method):
req = oauth.OAuthRequest.from_consumer_and_token(
consumer(), token=token,
http_url='http://twitter.com/oauth/authorize'
)
req.sign_request(signature_method, consumer(), token)
return req.to_url()
def get_authorized_token(token, signature_method=signature_method):
req = oauth.OAuthRequest.from_consumer_and_token(
consumer(), token=token,
http_url='https://twitter.com/oauth/access_token'
)
req.sign_request(signature_method, consumer(), token)
return oauth.OAuthToken.from_string(oauth_response(req))
def api(url, token, http_method='GET', **kwargs):
try:
return json.loads(oauth_response(oauth_request(
url, token, http_method=http_method, parameters=kwargs
)))
except: pass
return None
def verify_credentials(token):
return api('https://twitter.com/account/verify_credentials.json', token)