簡體   English   中英

HTTP 請求 Github 數據庫 API 通過 ZA7F5F35426B927411FC9231B56382173

[英]HTTP requests to Github Database API via Python keep returning 404

我最近一直在試驗 GitHub API。 目前,我正在嘗試從 Git 數據庫中為我的一個存儲庫訪問數據(特別是標簽),但稍后我希望能夠在對事物有更好的感覺后創建新的標簽對象。 我所說的 GitHub 數據庫 API 的意思是: https://docs.ZBF215181B51405/7213AZ.com/en4F6

我一直在嘗試在以下代碼中使用 http GET 請求 Git 標簽(如上述鏈接中指向的文檔中所指定)在以下代碼中(特別是標記為 #3 和 #4 的代碼塊):

import requests
import time
from requests import api

github_username = "XXXXX"
github_accessToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
github_repoName = "XXXXXXXXXXXXXXX"


print(end="\n\n\n")

# 1
# Getting basic user information
print("Getting basic user Information:")
gitHubAPI_URL_getUserInfo = f"https://api.github.com/users/{github_username}"
response = requests.get(gitHubAPI_URL_getUserInfo, auth=(github_username, github_accessToken))
data = response.json()
print("MetaData:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(response)
print(type(data))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Data:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for key, value in data.items():
    print(str(key) + ": " + str(value))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print()
print("====================================================================")
print()


# 2
# Getting tags
print("Getting tags:")
gitHubAPI_URL_getTags = f"https://api.github.com/repos/{github_username}/{github_repoName}/tags"
response = requests.get(gitHubAPI_URL_getTags, auth=(github_username, github_accessToken))
data = response.json()
print("MetaData:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(response)
print(type(data))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Data:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for element in data:
    print("///////")
    for key, value in element.items():
        print(str(key) + ": " + str(value))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print()


# 3
# Getting tags alternate attempt 1
print("Getting a particualr tag attempt 1 (Auth):")
gitHubAPI_URL_getTags_particular1 = f"https://api.github.com/repos/{github_username}/{github_repoName}/git/tags"
response = requests.get(gitHubAPI_URL_getTags_particular1, auth=(github_username, github_accessToken))
data = response.json()
print("MetaData:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(response)
print(type(data))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Data:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for key, value in data.items():
    print(str(key) + ": " + str(value))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print()
print("====================================================================")
print()


# 4 
# Getting tags alternate attempt 2
print("Getting a particualr tag attempt 2 (Headers):")
gitHubAPI_URL_getTags_particular2 = f"https://api.github.com/repos/{github_username}/{github_repoName}/git/tags/"
header = {"Authorization" : 'token ' + github_accessToken}
response = requests.get(gitHubAPI_URL_getTags_particular2, headers=header)
data = response.json()
print("MetaData:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(response)
print(type(data))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Data:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for key, value in data.items():
    print(str(key) + ": " + str(value))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print()
print("====================================================================")
print()


# 5
# Getting commits
print("Getting commits:")
gitHubAPI_URL_getCommits = f"https://api.github.com/repos/{github_username}/{github_repoName}/commits"
response = requests.get(gitHubAPI_URL_getCommits, auth=(github_username, github_accessToken))
data = response.json()
print("MetaData:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(response)
print(type(data))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Data:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for element in data:
    print("///////")
    for key, value in element.items():
        print(str(key) + ": " + str(value))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

然而,對於這些代碼塊,我不斷收到來自 GitHub 的 404 -“找不到資源”錯誤代碼。 Initially I thought this was an authentication issue as I was not providing authentication credentials at first and 404 error codes are what GitHub's REST API returns without proper authentication: https://docs.github.com/en/rest/overview/troubleshooting .

但即使添加了適當的授權,我仍然會返回 404。 我確信我已經正確填寫了身份驗證,因為每當我將任何憑證變量(例如 'github_accessToken' 的值)更改為某個無效值(例如更改個人訪問令牌值中的字符)時,我都會得到 401 - " Bad Credentials”在代碼塊 #1、#2 和 #5 上返回,以前可以正常工作。

我能得出的唯一結論是,在我請求數據的地方確實沒有數據,但這對我來說毫無意義,因為我知道存儲庫和我請求的數據存在,除非我誤解了事情並且存在GitHub 數據庫和 GitHub 存儲庫之間的區別

我能夠復制您的問題,並且我很確定您在代碼塊 #2 中的身份驗證是正確的,並且獲得404不是由於身份驗證。

根據GitHub API 關於標簽的文檔,您只能

  1. 創建標簽 object
    • 使用一些參數在/repos/{owner}/{repo}/git/tags上執行POST
  2. 獲取標簽
    • /repos/{owner}/{repo}/git/tags/{tag_sha}上執行GET

你做的不是這兩個操作。 我相信您正在嘗試執行獲取標簽的第二次操作,在這種情況下您的請求是錯誤的,因為您沒有在GET請求的TAG_SHA末尾提供 TAG_SHA。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM