使用 github API 为存储库加注星标

Starring a repository with the github API

我正在尝试使用 GithubAPI 为存储库加注星标。这是通过 PUT request to /user/starred/:owner/:repo 完成的。我尝试使用请求库在 python 中实现此功能,但它不起作用。这是一个最小的工作示例:

常量定义为GITHUB_API = api.github.comGITHUB_USER = the username of the owner of the repo to be starredGITHUB_REPO = the name of the repo to be starred

url = urljoin(GITHUB_API, (user + '/starred/' + GITHUB_USER + '/' + GITHUB_REPO))
r = requests.put(url,auth=(user,password))
print r.text

此代码导致错误:

{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}

我认为我遗漏了有关发出 PUT 请求的过程的一些基本知识。

这里的问题是你传递给 urljoin() 的参数。第一个参数应该是绝对值 URL,而第二个参数是相对值 URL。 urljoin() 然后从中创建一个绝对值 URL。

此外,"user" 在这种情况下应该是 URL 的文字部分,而不是用户名。

在这种情况下,我会完全放弃 urljoin()-函数,而是使用简单的字符串替换:

url = 'https://api.github.com/user/starred/{owner}/{repo}'.format(
          owner=GITHUB_USER,
          repo=GITHUB_REPO
      )