【问题标题】:How to get a file via GitHub APIs如何通过 GitHub API 获取文件
【发布时间】:2012-03-05 13:32:16
【问题描述】:

我需要获取托管在 GitHub 存储库中的文件的内容。我希望得到一个带有元数据的 JSON 响应。我已经尝试了许多带有 cURL 的 URL,但只得到了{"message":"Not Found"} 的响应。我只需要 URL 结构。如果它很重要,它来自 GitHub 上的一个组织。以下是我认为应该有效但无效的方法:

http://api.github.com/repos/<organization>/<repository>/git/branches/<branch>/<file>

【问题讨论】:

  • 简单 JSON 响应的三个请求?好法律。一点都不直观。当然还有更优雅的方式。
  • 这可能是他们 API 中最薄弱的部分之一。您可以使用他们的 Trees API 导航结构(在 Git Data in docs)。为了使用它,你需要一个 sha。你可以从回购分支中挖掘出来。也许像这样使用 raw.github.com 更容易? raw.github.com/:user/:repo/:branch/:filename 。您可以轻松地结合这两种方法来确定某个文件是否存在,然后获取它。
  • 是的,我几天前就知道了。不过,我需要文件结构。基本上,我想链接到我网站上的 Github 文件。将其视为我的 Github 文件的索引页面。

标签: api github


【解决方案1】:

正如描述(位于http://developer.github.com/v3/repos/contents/)所说:

/repos/:owner/:repo/contents/:path

ajax 代码将是:

$.ajax({
    url: readme_uri,
    dataType: 'jsonp',
    success: function(results)
    {
        var content = results.data.content;
    });

用正确的 /repos/:owner/:repo/contents/:path 替换 readme_uri。

【讨论】:

  • 这是新的吗?我发誓当我问的时候这不在这里。我查看了整个开发页面。谢谢。
  • 看起来 GitHub 正在发送以 Base64 编码的文件内容...
  • @taseenb 使用 https://raw.githubusercontent.com/:owner/:repo/master/:path 获取原始数据(二进制,而非 Base64)
  • @Peter 您在哪里找到您在评论中提到的链接?拯救了我的一天 :) 将 base64 编码的内容转换回原始内容太可怕了
  • @rohanagarwal 如果您浏览到 GitHub 上的文件,然后单击 Raw 链接,那么您将去那里。 What do raw.githubusercontent.com URLs represent?
【解决方案2】:

This GitHub API page 提供完整参考。读取文件的 API 端点:

https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}
{
  "encoding": "base64",
  "size": 5362,
  "name": "README.md",
  "content": "encoded content ...",
  "sha": "3d21ec53a331a6f037a91c368710b99387d012c1",
  ...
}
  • 考虑使用a personal access token
    • 速率限制(匿名最高每小时 60 个,认证最高每小时 5,000)read more
    • 启用访问私有存储库中的文件
  • 响应中的文件内容是base64编码的字符串

使用 curl,jq

通过 curl 和 jq 使用 GitHub 的 API 阅读 https://github.com/airbnb/javascript/blob/master/package.json

curl https://api.github.com/repos/airbnb/javascript/contents/package.json | jq -r ".content" | base64 --decode

使用 Python

在 Python 中使用 GitHub 的 API 阅读 https://github.com/airbnb/javascript/blob/master/package.json

import base64
import json
import requests
import os


def github_read_file(username, repository_name, file_path, github_token=None):
    headers = {}
    if github_token:
        headers['Authorization'] = f"token {github_token}"
        
    url = f'https://api.github.com/repos/{username}/{repository_name}/contents/{file_path}'
    r = requests.get(url, headers=headers)
    r.raise_for_status()
    data = r.json()
    file_content = data['content']
    file_content_encoding = data.get('encoding')
    if file_content_encoding == 'base64':
        file_content = base64.b64decode(file_content).decode()

    return file_content


def main():
    github_token = os.environ['GITHUB_TOKEN']
    username = 'airbnb'
    repository_name = 'javascript'
    file_path = 'package.json'
    file_content = github_read_file(username, repository_name, file_path, github_token=github_token)
    data = json.loads(file_content)
    print(data['name'])


if __name__ == '__main__':
    main()
  • 运行前定义环境变量GITHUB_TOKEN

【讨论】:

    猜你喜欢
    • 2018-02-02
    • 2023-03-19
    • 1970-01-01
    • 2014-09-21
    • 2017-06-12
    • 2023-03-15
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多