GithubHelp home page GithubHelp logo

dneuhaeuser-zalando / python-tokens Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zalando-stups/python-tokens

0.0 1.0 0.0 36 KB

Python library to manage OAuth access tokens

Home Page: https://pypi.python.org/pypi/stups-tokens

License: Other

Shell 3.32% Python 96.68%

python-tokens's Introduction

Python Tokens

Build Status Code Coverage Latest PyPI version License

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>

python-tokens's People

Contributors

hjacobs avatar jan-m avatar last-g avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.