suyash joshi
suyash joshi

Reputation: 61

getting info through API calls and authenticating

I want to make a Website that makes API calls to a server i have bought and we get back details about a user and use those details to create a authentication system. i need to use these few lines to do it import requests

url = "*************.com.login"

querystring = {"username":"username","password":"password"}

response = requests.request("GET", url, headers=headers, params=querystring)

jData = response.json()

i ham trying to integrate this with the inbuilt django authentication system. How should i do it.

any help appreciated Thanks..

i have tried something like this class AuthenticationBackend(backends.ModelBackend):

def authenticate(self, request, username=None, password=None, **kwargs):
    usermodel = get_user_model()
    try:

        #user = user.objects.get(username=username)
            # API Call
        lms_url = "http://"*************.com/login"
        querystring = {"username":username,"password":password}
        #queryString = {"username":username,"password":password}
        response = requests.request("POST", lms_url, params=querystring)
        jData = json.loads(response.text)

        if jData['code'] == '200':
            user = User.objects.get(username=username)
            return user
        else:
            return None

url = "*************.com/login"

querystring = {"username":"username","password":"password"}

response = requests.request("GET", url, headers=headers, params=querystring)

jData = response.json()

Upvotes: 1

Views: 45

Answers (2)

suyash joshi
suyash joshi

Reputation: 61

import requests

url = "******"

querystring = {"username":"****","password":"***"} # or any other parameter required for the API call

response = requests.request("GET", url, headers=headers, params=querystring)

jData = response.json()# the response is converted to JSON format

Upvotes: 0

Dziu Gas
Dziu Gas

Reputation: 47

If you would like to post requests you can use:

request = requests.post(f'127.0.0.1/api/name={name}&password={password}')

Upvotes: 0

Related Questions