Skip to content

Instantly share code, notes, and snippets.

@jpadilla
Last active January 1, 2016 02:19
Show Gist options
  • Save jpadilla/8078384 to your computer and use it in GitHub Desktop.
Save jpadilla/8078384 to your computer and use it in GitHub Desktop.
import urllib
import requests
client_id = '...'
client_secret = '...'
site = 'https://sopr.herokuapp.com'
redirect_uri = '...'
oauth_params = {
'redirect_uri': redirect_uri,
'client_id': client_id,
'response_type': 'code'
}
# 1. Redirect user to authorization URL
authorize_url = "{}/oauth/authorize?{}".format(
site, urllib.urlencode(oauth_params))
print authorize_url
# and get code query string after user is redirected to redirect_uri
code = "..."
# 2. Get Access token using code from step 1
data = {
'client_id': client_id,
'client_secret': client_secret,
'redirect_uri': redirect_uri,
'grant_type': 'authorization_code',
'code': code
}
response = requests.post("{}/oauth/token".format(site), data=data)
access_token = response.content['access_token']
print access_token
# 3. Use access token as parameter when calling API endpoint
params = {
'access_token': access_token
}
profile = requests.get("{}/api/v1/me.json".format(site), params=params).json()
print profile
require 'oauth2'
client_id = '...'
client_secret = '...'
redirect_uri = '...' # your client's redirect uri
base_url = "http://startupsofpuertorico.com"
client = OAuth2::Client.new(client_id, client_secret, :site => base_url)
client.auth_code.authorize_url(:redirect_uri => redirect_uri)
# => http://startupsofpuertorico.com/oauth/authorize?response_type=code&client_id=...&redirect_uri=...
code = "..." # code you got in the redirect uri
token = client.auth_code.get_token(code, :redirect_uri => redirect_uri)
# => <#OAuth2::AccessToken ...>
response = token.get('/api/v1/me.json')
JSON.parse(response.body)
# => { "username": "something", ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment