Chris
Chris

Reputation: 1939

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

I am trying to retrieve the raw contents of the README.rm file of a private repository on Github.

I'm extremely close, the only issue currently, is that I'm getting back a 401 unauthorized result.

Currently, I am trying to retrieve the file using:

        $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);

This makes a url similar to https://raw.github.com/chriscct7/Testplugin/master/README.md?login=chriscct7&token=e590999c1680dba1bcd5488658fa570eb6cbf53e&token_type=bearer

With the post return:

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 

The access token was made by using the provided OAuth example. I have checked, and my app on Github does have access to my private repositories.

Now, I know my method works, because if I use my personal login token:

$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'] ) );

This works, and returns the contents of the file fine. Note I did not use my real tokens in the code above for security reasons.

However, I need to make this work using the app's access_token.

Any ideas?

Upvotes: 3

Views: 2764

Answers (1)

raghava
raghava

Reputation: 246

This needs to be handled using the Repo contents API provided by GitHub. The file content would be in base64, withing the API's response.

Upvotes: 2

Related Questions