简体   繁体   中英

Python programming output is not as expected

I tried to automate the login for github api from "https://docs.python-requests.org/en/master/" I am getting error code 401 in response. Kindly guide me if my code is not good. Please find the code below. NOTE: Configuration.py, properties.ini, credentials.py are under Utilities package.

Login.py

import requests
import configparser
from Utilities.Credentials import *
from Utilities.configuration import *

url_login = get_Config() ['API'] ['endpoint']

github_Res = requests.get(url_login, auth= (getUsername() , getPassword()), )

print(url_login)

Status_Code = github_Res.status_code

print(Status_Code)

if Status_Code == 200:
    print("User logged in Successfully.")
else:
    print("User log in failed!!")

Configuration.py

import configparser


def get_Config():
    config = configparser.ConfigParser()
    config.read('Utilities/properties.ini')
    return config

credentials.py

def getUsername():
    return "***********@****.com"


def getPassword():
    return "********"

properties.ini

[API]
endpoint = https://api.github.com/user

It seems to me that you have two problems here:

  1. You're trying to make a GET request when authentication usually requires a POST request.
  2. You're sending the login credentials in the authentication header, not as form data.

Try doing it like this:

data = {"username": getUsername(), "password": getPassword()}
github_Res = requests.post(url_login, data = data)

Note that you'll probably still have issues because you haven't included the grant type or scope in your request.

UPDATE

I looked up GitHub'sAPI documentation and it appears that it doesn't support username-password authentication anymore. You should probably rethink your solution to use a username and personal access token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM