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

时间:2015-06-14 00:08:59

标签: github put 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请求过程的基本信息。

1 个答案:

答案 0 :(得分:0)

此处的问题是您传递给urljoin()的参数。第一个参数应该是绝对URL,而第二个参数是相对URL。然后urljoin()创建一个绝对URL。

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

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

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