Download this file

osp-auth-example.py    40 lines (31 with data), 1.1 kB

 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
#!/usr/bin/python
"""
Example on how to use the Allura API w/ auth
"""
import hmac, hashlib
from datetime import datetime
import urllib
from urllib2 import urlopen
from urlparse import urlparse
class Signer(object):
def __init__(self, secret_key, api_key):
self.secret_key = secret_key
self.api_key = api_key
def __call__(self, path, params):
if self.api_key is None:
return params
params.append(('api_key', self.api_key))
params.append(('api_timestamp', datetime.utcnow().isoformat()))
message = path + '?' + urllib.urlencode(sorted(params))
digest = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
params.append(('api_signature', digest))
return params
SECRET_KEY = ''
API_KEY = ''
URL = 'https://opensourceprojects.eu/rest/p/pristine/wp1/wp1tickets/'
def main():
sign = Signer(SECRET_KEY, API_KEY)
queryparams = sign(urlparse(URL).path, [])
result = urlopen(URL+'?'+urllib.urlencode(queryparams))
print(result.read())
main()