Comments (3)
-
hard to tell - you need to show your python client code as well
-
user = authenticate(username=username, password=password)
- is not enogh if you want to use django session - you need as well calllogin(request, user)
to store it in session - documentation
from django-ninja.
Thanks for your kind response. I got it working with Bearer, by creating a config file containing username and password that are then stored as env variables. These are then retrieved and used to submit my request.
One more question:
could I omit username and password arguments from my url? And, instead of declaring them explicitly, add them in the header?
Thanks again.
import requests
#This is to log requests to database
from dotenv import dotenv_values
from dotenv import load_dotenv
import os
fileName = 'Some_config_file.txt'
path = '/home'
#Define config file
config = dotenv_values(os.path.join(path, fileName))
# Load the environment variables from the text file
load_dotenv(os.path.join(path, fileName))
# Read the credentials from the environment variables
username = os.getenv("_USERNAME")
password = os.getenv("_PASSWORD")
token = os.getenv("_TOKEN")
# Create the header with the credentials
headers = {
"Authorization": f"Bearer {token}"
}
# Construct the URL without the credentials <--- omit user and passwords, by including in header?
url = "http://127.0.0.1:8000/endpoint/bearer?username={}&password={}".format(username, password)
#Submit Request
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
for key, value in data.items():
print("{:15s} : {:10s}".format(str(key), str(value)))
else:
print(f"Request failed with status code {response.status_code}")
from django-ninja.
@domcyi24 yeah yoprobably better use post method for authentication
from ninja import Schema, Header
from ninja.security import HttpBearer
class TokenBearer(HttpBearer):
def authenticate(self, request, token):
if token == "supersecret":
return token
@api = NinjaAPI(auth=TokenBearer())
class LoginSchema(Schema):
username: str
password: str
@api.post("/endpoint/bearer")
def user_login(request, body: LoginSchema):
username = body.username
password...
...login...
then your client code should look like this:
headers = {
"Authorization": f"Bearer {token}"
}
payload = {
"username": "your_username",
"password": "your_password"
}
url = "http://127.0.0.1:8000/endpoint/bearer"
response = requests.post(url, json=payload, headers=headers)
from django-ninja.
Related Issues (20)
- [BUG] Mocked request object not using HttpRequest as spec
- too many values to unpack (expected 2) HOT 8
- [BUG] Running tests using Schema model generates warning
- [BUG] Awkward router behaviour when path is like '/{some_var}' HOT 1
- After adding the blacklist successfully, the token is still authenticated successfully HOT 1
- [BUG] Sync-only Authentication Callbacks not Working on Async Operations HOT 1
- [BUG] Mock Request Generated by Test Client Doesnโt Support auser()
- RealWorld Demo for Django Ninja HOT 2
- Is there a way to use swagger and redoc together? HOT 1
- Websocket please!
- Inconsistent Documentation Regarding Resolver
- Easy way to change behavior of `BodyModel`? HOT 2
- [BUG] Converter `uuid` is already registered - deprecated in django 6.0
- Make 'exclude_none' and other options easier to enable per router
- [BUG] Field serialized when using repr=False HOT 1
- How to create an alias for a field created via annotation HOT 2
- [BUG] model_dump defaults to `mode=python`
- How can I use resolve_<field> for async api?
- Mypy complaining about PatchDict HOT 1
- [Question] Unable to cache paginated queryset `PicklingError` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-ninja.