Last active
January 1, 2016 02:19
-
-
Save jpadilla/8078384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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