-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindings_client.py
executable file
·66 lines (58 loc) · 2.14 KB
/
findings_client.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
import time
import hashlib
import random
import urllib
import urllib2
import simplejson
class FindingsAPIClient:
URLS = {
"auth_url": "https://findings.com/oauth2/authorize/?client_id=%s&scope=%s&state=%s&response_type=%s&redirect_uri=%s",
"token": "https://findings.com/oauth2/token/",
"callback_url": "http://YOURSITE/apiclient/callback/",
"create_clip": "https://findings.com/api/v1/clip/?access_token=%s",
"clips": "https://findings.com/api/v1/clip/?format=json&key=%s"
}
def __init__(self, key, secret):
self.key = key
self.secret = secret
self.callback = self.URLS["callback_url"]
def gen_nonce(self):
h = hashlib.md5()
random_number = ''.join(str(random.randint(0, 9)) for i in range(40))
h.update(str(time.time()) + str(random_number))
return h.hexdigest()
def auth_url(self, scope=""):
# generate the auth-url for the initial code request
return self.URLS["auth_url"] % (
self.key,
scope,
self.gen_nonce(),
"code",
self.callback
)
def get_access_token(self, code, state):
# given a code and a state from the api server
# ask the api server for an access_token
url = self.URLS["token"]
values = {
"client_id": self.key,
"client_secret": self.secret,
"code": code,
"state": state,
"grant_type": "authorization_code",
"response_type": "token",
"redirect_uri": self.callback,
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
# eg {'access_token': '82d2d10e55', 'token_type': 'bearer',
# 'expires_in': 31536000, 'refresh_token': 'd70f32b995', 'scope': ''}
return simplejson.loads(result)
def fetch_clips(self):
url = self.URLS["clips"] % (self.key)
f = urllib2.urlopen(url)
data = simplejson.loads(f.read())
clips = [n for n in data.get("objects", [])]
return clips