Skip to content

Instantly share code, notes, and snippets.

@mozillazg
Last active January 1, 2021 11:31
Show Gist options
  • Save mozillazg/4af649ff88612b2de7c7 to your computer and use it in GitHub Desktop.
Save mozillazg/4af649ff88612b2de7c7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from oauthlib.oauth2 import (
FatalClientError, OAuth2Error, TokenExpiredError, MobileApplicationClient
)
from requests_oauthlib import OAuth2Session, TokenUpdated
client_id = 'xxxx'
client_secret = 'yyyy'
redirect_uri = 'https://api.shanbay.com/oauth2/auth/success/'
authorization_base_url = 'https://api.shanbay.com/oauth2/authorize/'
token_url = 'https://api.shanbay.com/oauth2/token/'
token_file = 'token.json'
def userinfo(api):
r = api.get('https://api.shanbay.com/account/')
print r.json()
def get_token():
client = MobileApplicationClient(client_id)
api = OAuth2Session(client=client, redirect_uri=redirect_uri)
authorization_url, state = api.authorization_url(authorization_base_url)
print 'Please go here and authorize,', authorization_url
redirect_response = raw_input('Paste the full redirect URL here:')
token = api.token_from_fragment(redirect_response)
with open(token_file, 'w') as f:
f.write(json.dumps(token, indent=2))
return api
try:
with open(token_file) as f:
token = json.loads(f.read())
api = OAuth2Session(client_id, token=token)
userinfo(api)
except (
TokenExpiredError, TokenUpdated, OAuth2Error, FatalClientError, ValueError
) as e:
api = get_token()
userinfo(api)
@mozillazg
Copy link
Author

Change client_id and client_secret to yours.

@wbingli
Copy link

wbingli commented Jul 7, 2015

where I can get client_id and client_secret from?

@qiwihui
Copy link

qiwihui commented Jul 13, 2015

@wbinglee you can go to here: shanbay developer application to get your client_id and client_secret. To be honest, it is a little hard to find since it is not very obviously in the developer document. Once i thought you need to send an mail to get them. 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment