当我尝试访问通过Node.js与其合作的Github私有存储库时获得404

时间:2019-11-22 19:41:46

标签: javascript node.js github

我有一个独特的处境。我有近100个私有Github仓库,其所有者是不同的人。他们使我成为了合作者。我可以通过命令行手动下载每个存储库。但是我正在尝试通过Node.js自动化它们。但是无论出于什么原因,我都得到404。我尝试了各种方法,但无济于事。 非常感谢您的帮助!

PS:我已经生成了一个个人访问令牌,并尝试了以下方法

方法1:

curl -i -H "Authorization: token <personal-access-token>"     https://api.github.com/users/username/privateRepo

Returns:
404

方法2:

curl -i https://api.github.com/repos/applitools/website -u <username>:<personal access token>

Returns:
404

方法3:NodeGit: 参考:https://gist.github.com/mojavelinux/173a14a1f78ab5e9b804452b3d055d05

var GITHUB_TOKEN = "<Personal Access Token>";
var cloneURL = "https://github.com/username/repo.git";
var localPath = __dirname + "/gitRepos/b";
var cloneOptions = {};
cloneOptions.fetchOpts = {
  callbacks: {
    certificateCheck: function() {
      return 0;
    },
    credentials: function() {
      return NodeGit.Cred.userpassPlaintextNew(
        "myGithubName",
        "<Personal Access Token>"
      );
    }
  }
};

async function temp() {
  try {
    await NodeGit.Clone(cloneURL, localPath, cloneOptions);
  } catch (e) {
    console.log(e);
  }
}

temp(); //Returns 404

方法4:通过Github API 参考:https://medium.com/@milosbejda/download-private-github-repo-using-node-js-7f116593401c

var owner = "<Repo-Owner>";
var repo = "<Private-Repo>";
var branch = "master";
var accessToken = "My-Personal-Access-Token";

var options = {
  method: "GET",
  url: `https://api.github.com/repos/${owner.toLowerCase()}/${repo.toLowerCase()}/tarball/${branch}?access_token=${accessToken}`,
  headers: {
    Accept: "application/vnd.github.v3.raw",
    "User-Agent": "mbejda.com"
  }
};

request(options, function(error, response, body) {
  if (error || response.statusCode !== 200) {
    try {
      console.log("failed to get repo ", response.statusCode);
    } catch (e) {}
    ///'Content-Type', 'application/zip'
    console.log(error);
  } else {
    console.log(body);
    //// we got the repo!
  }
});

0 个答案:

没有答案