通过access_token读取 Github 上私有存储库中文件的原始内容


Read Raw Contents of File in Private Repo on Github via access_token

我正在尝试检索 Github 上私有存储库的 README.rm 文件的原始内容。

我非常接近,目前唯一的问题是我正在返回 401 未经授权的结果。

目前,我正在尝试使用以下方法检索文件:

        $query = $this->config['raw_url']  . '/README.md?login=chriscct7&token='.$this->config['access_token'] ;
        //$query = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $query );
        $query = $query.'&token_type=bearer';
        $raw_response = wp_remote_get( $query, array( 'sslverify' => $this->config['sslverify'] ) );
        var_dump($query, $raw_response);

这使得网址类似于 https://raw.github.com/chriscct7/Testplugin/master/README.md?login=chriscct7&token=e590999c1680dba1bcd5488658fa570eb6cbf53e&token_type=bearer

随着帖子返回:

array (size=5)
  'headers' => 
    array (size=16)
      'date' => string 'Wed, 23 Jan 2013 13:58:50 GMT' (length=29)
      'server' => string 'GitHub.com' (length=10)
      'content-type' => string 'text/html; charset=utf-8' (length=24)
      'status' => string '401 Unauthorized' (length=16)
      'x-ratelimit-remaining' => string '100' (length=3)
      'x-runtime' => string '12' (length=2)
      'x-ratelimit-limit' => string '100' (length=3)
      'content-length' => string '1' (length=1)
      'accept-ranges' => string 'bytes' (length=5)
      'age' => string '0' (length=1)
      'via' => string '1.1 varnish' (length=11)
      'x-served-by' => string 'cache-c32-CHI' (length=13)
      'x-cache' => string 'MISS' (length=4)
      'x-cache-hits' => string '0' (length=1)
      'cache-control' => string 'no-cache' (length=8)
      'connection' => string 'close' (length=5)
  'body' => string ' ' (length=1)
  'response' => 
    array (size=2)
      'code' => string '401' (length=3)
      'message' => string 'Unauthorized' (length=12)
  'cookies' => 
    array (size=0)
      empty
  'filename' => null 

访问令牌是使用提供的 OAuth 示例创建的。我已经检查过了,我在 Github 上的应用程序确实可以访问我的私有仓库。

现在,我知道我的方法有效,因为如果我使用我的个人登录令牌:

$query = 'https://raw.github.com/chriscct7/Testplugin/master/README.md?login=chriscct7&token=e4d293652a9081d79e582984a3f32dc7';
$raw_response = wp_remote_get( $query, array( 'sslverify' => $this->config['sslverify'] ) );

这有效,并返回文件的内容。注意 出于安全原因,我没有在上面的代码中使用我的真实令牌。

但是,我需要使用应用程序的access_token来完成这项工作。

有什么想法吗?

这需要使用 GitHub 提供的 Repo 内容 API 进行处理。文件内容将位于 base64 中,具有 API 的响应。