#!/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)