query = "https://api.github.com/repos/rhinstaller/anaconda/tags"
response = requests.get(query,auth=(self.username,self.token))
response.raise_for_status() # raises exception when not a 2xx
if response.status_code != 204:
try:
data = response.json()
print("data = ",data)
except:
print("0")
上面的代碼總是將資料回傳為空,而response = requests.get(query)在超過速率之前仍然可以作業。
這背后的原因可能是什么?以前,github API 在 Python 中可以很好地處理這種基于元組的身份驗證。
編輯:print(response.__dict__)產量:
{'_content': b'{"message":"Bad credentials","documentation_url":"https://docs.github.com/rest"}', '_content_consumed': True, '_next': None, 'status_code': 401, 'headers': {'Server': 'GitHub.com', 'Date': 'Sat, 22 Oct 2022 14:01:13 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '80', 'X-GitHub-Media-Type': 'github.v3; format=json', 'X-RateLimit-Limit': '60', 'X-RateLimit-Remaining': '56', 'X-RateLimit-Reset': '1666449620', 'X-RateLimit-Used': '4', 'X-RateLimit-Resource': 'core', 'Access-Control-Expose-Headers': 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset', 'Access-Control-Allow-Origin': '*', 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains; preload', 'X-Frame-Options': 'deny', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '0', 'Referrer-Policy': 'origin-when-cross-origin, strict-origin-when-cross-origin', 'Content-Security-Policy': "default-src 'none'", 'Vary': 'Accept-Encoding, Accept, X-Requested-With', 'X-GitHub-Request-Id': 'D2F0:13300:1D277E2:1D98791:6353F7A9'}, 'raw': <urllib3.response.HTTPResponse object at 0x7f596e77da90>, 'url': 'https://api.github.com/repos/rhinstaller/anaconda/tags', 'encoding': 'utf-8', 'history': [], 'reason': 'Unauthorized', 'cookies': <RequestsCookieJar[]>, 'elapsed': datetime.timedelta(microseconds=176914), 'request': <PreparedRequest [GET]>, 'connection': <requests.adapters.HTTPAdapter object at 0x7f596e76cbd0>}
uj5u.com熱心網友回復:
該錯誤表明您的憑據不正確
import requests
from requests.auth import HTTPBasicAuth
# Making a get request
response = requests.get('https://api.github.com/user, ',
auth = HTTPBasicAuth('user', 'token'))
# print request object
print(response)
參考:https ://www.geeksforgeeks.org/authentication-using-python-requests/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522388.html
