Giter Club home page Giter Club logo

Comments (3)

vitalik avatar vitalik commented on September 7, 2024 1
  1. hard to tell - you need to show your python client code as well

  2. user = authenticate(username=username, password=password) - is not enogh if you want to use django session - you need as well call login(request, user) to store it in session - documentation

from django-ninja.

domcyi24 avatar domcyi24 commented on September 7, 2024

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.

vitalik avatar vitalik commented on September 7, 2024

@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)

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.