简体   繁体   English

通过 JavaScript 从私有 GitHub 存储库获取文件内容

[英]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.我想访问通过 Node.js 上传到 GitHub 的文件的内容。

The GitHub repository is private, so I have generated an access token at https://github.com/settings/tokens GitHub 存储库是私有的,所以我在https://github.com/settings/tokens生成了一个访问令牌

Unfortunately, I keep getting a 404 – Not found error.不幸的是,我不断收到 404 – Not found 错误。 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);

Thanks to the comment of @bhavesh27 I figured, I was missing a "token " in my header.感谢@bhavesh27 的评论,我想,我的 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM