Ubuntu Pastebin

Paste from David_Orange at Wed, 8 Feb 2017 11:00:34 +0000

Download as text
 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
#!/usr/bin/env python3.5


from pprint import pprint as pp
import requests
import oauth.oauth as oauth
import uuid
import httplib2

site = 'http://127.0.0.1:5555/MAAS/api/2.0'
uri = '/nodes/?op=list'
api_key = 'SeAtrC2XCMTnrkJQEX:UQtFzksutMwnAH8kuK:v3vcf6rR897xJyTgujmuy9zvG7ymaQBt'
(consumer_key, key, secret) = api_key.split(':')

def get_auth_headers(url, apikey):
    consumer_key, key, secret = apikey.split(':')
    resource_tok_string = "oauth_token_secret=%s&oauth_token=%s" % (
        secret, key)
    resource_token = oauth.OAuthToken.from_string(resource_tok_string)
    consumer_token = oauth.OAuthConsumer(consumer_key, "")

    oauth_request = oauth.OAuthRequest.from_consumer_and_token(
        consumer_token, token=resource_token, http_url=site,
        parameters={'oauth_nonce': uuid.uuid4().hex})
    oauth_request.sign_request(
        oauth.OAuthSignatureMethod_PLAINTEXT(), consumer_token,
        resource_token)
    headers = oauth_request.to_header()
    headers['Accept'] = 'application/json'
    return headers

apikey = api_key
url = site+uri
response = requests.get(url, headers=get_auth_headers(url, apikey))
pp(response.text)
Download as text