public
Created

Redu Client with OAuth

  • Download Gist
redu_client.py
Python
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
import urlparse
import oauth2 as oauth
import requests
 
# First of all fill in your consumer key and secret you get when
# you create your application at Redu
 
consumer_key = 'YOUR_CONSUMER_KEY_HERE'
consumer_secret = 'YOUR_CONSUMER_SECRET_HERE'
 
request_token_url = 'http://www.redu.com.br/oauth/request_token'
access_token_url = 'http://www.redu.com.br/oauth/access_token'
authorize_url = 'http://www.redu.com.br/oauth/authorize'
 
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
 
# Step 1: Get a request token. This is a temporary token that is used for
# having the user authorize an access token and to sign the request to obtain
# said access token.
 
resp, content = client.request(request_token_url, "GET")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
 
request_token = dict(urlparse.parse_qsl(content))
 
print "Request Token:"
print "oauth_token = %s" % request_token['oauth_token']
print "oauth_token_secret = %s" % request_token['oauth_token_secret']
print
 
# Step 2: Redirect to Redu. Since this is a CLI script we do not redirect.
# In a web application you would redirect the user to the URL below.
print "Go to the following link in your browser:"
print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
print
 
# After the user has granted access to you, the consumer, the Redu server
# will show him an alphanumeric code to enter in our application
accepted = 'n'
while accepted.lower() == 'n':
accepted = raw_input('Have you authorized me? (y/n) ')
oauth_verifier = raw_input('What is the PIN? ')
 
# You use this token to make a new request and ask for the access token. You
# should store this access token somewhere safe, like a database, for future use.
token = oauth.Token(request_token['oauth_token'],
request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
 
resp, content = client.request(access_token_url, "POST")
 
access_token = dict(urlparse.parse_qsl(content))
 
print "Access Token:"
print "oauth_token = %s" % access_token['oauth_token']
print "oauth_token_secret = %s" % access_token['oauth_token_secret']
 
 
# You may now access protected resources using the access tokens above.
# Here you can see how to create a client, fetch a list of AVAs from Redu
# And create a new one then
 
 
# creates the client with the token
client = requests.session(params={"oauth_token":access_token["oauth_token"]}, headers = {'content-type': 'application/json'})
 
print "Fetching Environments"
e = client.get("http://localhost:3000/api/environments.json")
print e.content
 
print "Creating a new Environment"
environment = {}
environment['name'] = raw_input('Environment Name?')
environment['path'] = raw_input('Environment Path?')
environment['initials'] = raw_input('Environment Initials?')
payload = {"environment":environment}
 
print "Wait for it..."
 
e = client.post("http://localhost:3000/api/environments.json", data=payload)
print e.content

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.