Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Python library to manage OAuth access tokens

Project Description

A Python library that keeps OAuth 2.0 service access tokens in memory for your usage.

Installation

$ sudo pip3 install --upgrade stups-tokens

Usage

import requests
import time
import tokens

# will use OAUTH2_ACCESS_TOKEN_URL environment variable by default
# will try to read application credentials from CREDENTIALS_DIR
tokens.configure(url='https://example.com/access_tokens')
tokens.manage('example', ['read', 'write'])
tokens.start()

tok = tokens.get('example')

requests.get('https://example.org/', headers={'Authorization': 'Bearer {}'.format(tok)})

time.sleep(3600) # make the token expire

tok = tokens.get('example') # will refresh the expired token
requests.get('https://example.org/', headers={'Authorization': 'Bearer {}'.format(tok)})

This library also allows reading tokens directly from a file. The token needs to be in a file name ${CREDENTIALS_DIR}/${TOKEN_NAME}-secret:

import tokens

# the environment variable CREDENTIALS_DIR must be set correctly
tokens.configure(from_file_only=True)
tokens.manage('full-access')
tok = tokens.get('full-access')

requests.get('https://example.org/', headers={'Authorization': 'Bearer {}'.format(tok)})

Local testing

The “tokens” library allows injecting fixed OAuth2 access tokens via the OAUTH2_ACCESS_TOKENS environment variable. This allows testing applications using the library locally with personal OAuth2 tokens (e.g. generated by “zign”):

$ MY_TOKEN=$(zign token -n mytok)
$ export OAUTH2_ACCESS_TOKENS=mytok=$MY_TOKEN
$ ./myapp.py # start my local Python app using the tokens library

Releasing

Uploading a new version to PyPI:

$ ./release.sh <NEW-VERSION>
Release History

Release History

This version
History Node

1.1.19

History Node

1.0.19

History Node

1.0.18

History Node

1.0.17

History Node

1.0.16

History Node

1.0.15

History Node

1.0.14

History Node

1.0.13

History Node

1.0.12

History Node

1.0.6

History Node

0.7

History Node

0.6

History Node

0.5

History Node

0.4

History Node

0.3

History Node

0.2

History Node

0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
stups_tokens-1.1.19-py3-none-any.whl (6.1 kB) Copy SHA256 Checksum SHA256 3.5 Wheel Jan 4, 2017
stups-tokens-1.1.19.tar.gz (4.5 kB) Copy SHA256 Checksum SHA256 Source Jan 4, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting