https://github.com/zalando-stups/python-tokens
Python library to manage OAuth access tokens
https://github.com/zalando-stups/python-tokens
bearer-tokens oauth2
Last synced: 3 months ago
JSON representation
Python library to manage OAuth access tokens
- Host: GitHub
- URL: https://github.com/zalando-stups/python-tokens
- Owner: zalando-stups
- License: other
- Created: 2015-07-16T13:50:53.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-03-19T21:59:23.000Z (over 6 years ago)
- Last Synced: 2025-07-02T08:58:16.709Z (3 months ago)
- Topics: bearer-tokens, oauth2
- Language: Python
- Homepage: https://pypi.python.org/pypi/stups-tokens
- Size: 34.2 KB
- Stars: 14
- Watchers: 13
- Forks: 8
- Open Issues: 8
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
=============
Python Tokens
=============.. image:: https://travis-ci.org/zalando-stups/python-tokens.svg?branch=master
:target: https://travis-ci.org/zalando-stups/python-tokens
:alt: Build Status.. image:: https://coveralls.io/repos/zalando-stups/python-tokens/badge.svg
:target: https://coveralls.io/r/zalando-stups/python-tokens
:alt: Code Coverage.. image:: https://img.shields.io/pypi/v/stups-tokens.svg
:target: https://pypi.python.org/pypi/stups-tokens/
:alt: Latest PyPI version.. image:: https://img.shields.io/pypi/l/stups-tokens.svg
:target: https://pypi.python.org/pypi/stups-tokens/
:alt: LicenseA Python library that keeps OAuth 2.0 service access tokens in memory for your usage.
Installation
============.. code-block:: bash
$ sudo pip3 install --upgrade stups-tokens
Usage
=====.. code-block:: python
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}-token-secret``:
.. code-block:: python
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"):.. code-block:: bash
$ MY_TOKEN=$(zign token -n mytok)
$ export OAUTH2_ACCESS_TOKENS=mytok=$MY_TOKEN
$ ./myapp.py # start my local Python app using the tokens libraryReleasing
=========Uploading a new version to PyPI:
.. code-block:: bash
$ ./release.sh