Skip to content

Instantly share code, notes, and snippets.

Created March 16, 2017 16:30
Show Gist options
  • Save anonymous/a3d4b578f4ac49acb26f59ec2ea8b273 to your computer and use it in GitHub Desktop.
Save anonymous/a3d4b578f4ac49acb26f59ec2ea8b273 to your computer and use it in GitHub Desktop.
Interacting with the Narro.co API with Python
"""
Interacting with the Narro.co API with Python
### Implementation
1. ask narro for authorization_code
2. visit the url to grant permission
3. paste the authorization_code into the script
4. exchange the authorization_code + secret_key for an access token
5. use the access token
"""
import requests
CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
CLIENT_SECRET = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
CLIENT_URI = "http://mysite.com/narro/callback.php"
REDIRECT_URI = 'http://mysite.com/narro/redirect.php'
AUTHORIZE_URL = "https://www.narro.co/oauth2/authorize/"
ACCESS_TOKEN_URL = "https://narro.co/oauth2/token/"
# 1. ask narro for authorization_code
r = requests.post('{}?grant_type=authorization_code&client_id={}&redirect_uri={}&response_type=code'
.format(AUTHORIZE_URL, CLIENT_ID, REDIRECT_URI))
# 2. visit the url to grant permission
print(r.url)
print("\n")
# 3. paste the authorization_code into the script
SESSION_CODE = input("do you have the pin?")
# 4. exchange the authorization_code + secret_key for an access token
q = requests.post(
ACCESS_TOKEN_URL,
data={
'grant_type': 'authorization_code',
'code': SESSION_CODE,
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'redirect_uri': REDIRECT_URI
}
)
print("\n")
print(q)
# As I understand it, this should return a JSON response that I could see with q.json(). Nope, 404.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment