Created
October 8, 2013 01:11
-
-
Save djuretic/6877868 to your computer and use it in GitHub Desktop.
Skritter API: get vocabs
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
from base64 import b64encode | |
from pprint import pprint | |
# pip install requests | |
import requests | |
# Configuration variables, see examples on http://beta.skritter.com/api/v0/docs/authentication | |
OAUTH_CLIENT_NAME = '' | |
OAUTH_CLIENT_SECRET = '' | |
# which account do you want to access? | |
USER_NAME = '' | |
USER_PASSWORD = '' | |
BASE_URL = 'http://beta.skritter.com/api/v0' | |
def auth_headers(client_id, client_secret): | |
# using encode/decode for python 3 compatibility | |
credentials = ("%s:%s" % (client_id, client_secret)).encode("utf-8") | |
credentials = b64encode(credentials).decode('utf-8') | |
credentials = "basic %s" % credentials | |
return {'AUTHORIZATION': credentials} | |
def auth_parameters(client_id, username, password): | |
return { | |
'grant_type': 'password', | |
'client_id': client_id, | |
'username': username, | |
'password': password, | |
} | |
def get_access_token(): | |
params = auth_parameters(OAUTH_CLIENT_NAME, USER_NAME, USER_PASSWORD) | |
headers = auth_headers(OAUTH_CLIENT_NAME, OAUTH_CLIENT_SECRET) | |
response = requests.get('%s/oauth2/token' % BASE_URL, params=params, headers=headers) | |
response = response.json() | |
access_token = response.get('access_token') | |
return access_token | |
def get_vocabs(token, limit=5): | |
params = { | |
'limit': limit, | |
'bearer_token': token | |
} | |
headers = { | |
'User-Agent': 'gzip', | |
'Accept-Encoding': 'gzip' | |
} | |
response = requests.get('%s/vocabs' % BASE_URL, params=params, headers=headers) | |
return response.json()['Vocabs'] | |
if __name__ == '__main__': | |
token = get_access_token() | |
vocabs = get_vocabs(token) | |
pprint(vocabs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment