Django app for OAuth 2.0 authentication with requests
Project description
django-oauth-requests
OAuth 2.0 authentication with Django and requests made simple.
This app is for authenticating with remote APIs that use OAuth 2.0. It saves issued tokens to a database and automatically takes care of refreshing them after their expiration.
Installation
Use a package manager to install it, for example using this command in your terminal:
pip install django-oauth-requests
Setup
First, add oauth_requests
into your INSTALLED_APPS
in the settings.py
file:
INSTALLED_APPS = [
…
"oauth_requests",
]
Then, add a configuration for all the services you plan to use, like this sample:
OAUTH_REQUESTS = {
"paypal": {
"client_id": "your-paypal-client_id",
"client_secret": "your-paypal-client_secret",
"url": "https://api-m.sandbox.paypal.com/v1/oauth2/token",
},
}
In this example, we only defined one service called paypal
. We should put there issued CLIENT_ID
and CLIENT_SECRET
strings under their respective keys. And url
should point to the authentication endpoint for issuing tokens.
And finally, we should run Django migration to create tables for storing issued tokens. We can accomplish this by running the following command in our terminal:
python manage.py migrate oauth_requests
Usage
In the actual code, use the OAuth
class to issue OAuth 2.0 token for your service and start to use it with requests
:
import requests
from oauth_requests import OAuth
oauth = OAuth("paypal")
# start sending requests using the issued token
response = requests.get("https://api-m.sandbox.paypal.com/v2/invoicing/invoices", auth=oauth)
# optionally, you can use requests.Session
session = requests.Session()
session.auth = oauth
response = session.get("https://api-m.sandbox.paypal.com/v2/invoicing/invoices")
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
Built Distribution
File details
Details for the file django_oauth_requests-1.0.1.tar.gz
.
File metadata
- Download URL: django_oauth_requests-1.0.1.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f9b060f45ed5de360fcd1bf0da2d41a5cc922d50421cc7b7f4b2a0a9a4bba4e |
|
MD5 | fc94a632e866d92294715cf8b128e524 |
|
BLAKE2b-256 | 2d087408c01127750a32d10287b9f347940994f3f203f59fb5327caead2845e1 |
File details
Details for the file django_oauth_requests-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_oauth_requests-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 131efa6ba191d489e6d1da6761989f61ee3f35ad718ec3c138f84dd4330868f0 |
|
MD5 | fb8e7056700df773ff03cd651ba8eeed |
|
BLAKE2b-256 | 2ed10820818250b1cbcc3dc153511d22913f2812a8b2ae49f0eea1d76497ea58 |