Python library to manage OAuth access tokens
Project description
=============
Python Tokens
=============
A 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)})
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 library
Python Tokens
=============
A 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)})
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 library
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
stups-tokens-0.4.tar.gz
(2.9 kB
view hashes)
Built Distribution
Close
Hashes for stups_tokens-0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7dfc42c73915d0ea626512f984b51dd24d74317bc8357654e81ff2226a0b47e |
|
MD5 | 564643469aac102f423daaab3ca5d991 |
|
BLAKE2b-256 | ca9cfb8c0b8b1c9b16f16f452cdc75a6a5802caf9f48e9c966125279480fc597 |