Skip to content

Instantly share code, notes, and snippets.

@djuretic
Created Oct 8, 2013
Embed
What would you like to do?
Skritter API: get vocabs
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