Jonas_Hess
Jonas_Hess

Reputation: 2018

Get file content from private GitHub repository via JavaScript

I would like to access the content of a file I uploaded to GitHub via Node.js.

The GitHub repository is private, so I have generated an access token at https://github.com/settings/tokens

Unfortunately, I keep getting a 404 – Not found error. What am I doing wrong?

const request = require('request');

const URL = 'https://raw.githubusercontent.com/myuser/myrepo/master/myfile.js';
const TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

var options = {
  url: URL,
  headers: {
    'Authorization': TOKEN
  }
};

function callback(error, response, body) {
      console.log(response.statusCode);
      console.error(error);
      console.log(body);
}

request(options, callback);

Upvotes: 2

Views: 3315

Answers (1)

Jonas_Hess
Jonas_Hess

Reputation: 2018

Thanks to the comment of @bhavesh27 I figured, I was missing a "token " in my header.

const request = require('request');

const URL = 'https://raw.githubusercontent.com/myuser/myrepo/master/myfile.js';
const TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

var options = {
  url: URL,
  headers: {
    'Authorization': 'token ' + TOKEN
  }
};

function callback(error, response, body) {
      console.log(response.statusCode);
      console.error(error);
      console.log(body);
}

request(options, callback);

Upvotes: 3

Related Questions