如何解决(nodegit) 如何使用令牌从/向私有 Github 存储库读取/写入
我从网上复制了一些代码,并使其工作以克隆并推送到我自己在 github 上的存储库之一。使用相同的代码,我尝试对现在由我拥有但由我所属的组织并且我拥有写入权限的存储库执行相同操作。这失败了 403。
这是我使用的代码。
GithubClient.js
var fs = require('fs');
var NodeGit = require('nodegit');
var config = require('./github-config.js');
var cloneOpts = {
callbacks: {
certificateCheck: () => {
return 1;
},credentials: function(url,username) {
console.log('creds for' + username);
return nodegit.Cred.userpassplaintextNew(config.token,"x-oauth-basic");
}
}
};
cloneOpts.fetchOpts = {
callbacks: cloneOpts.callbacks
};
class GitClient {
static clone(options) {
cloneOpts.checkoutBranch = options.branch;
return NodeGit.Clone(options.remote,options.local,cloneOpts)
.catch(err => console.log(err));
}
static addAndCommit(repo,filesArray,msg) {
return repo.createCommitOnHead(
filesArray,NodeGit.Signature.create(config.realname,config.email,new Date().getTime(),0),msg
);
}
static push(repo,repoName,remoteName,refs) {
return repo.getRemote(remoteName || 'origin')
.then(remote => {
return remote.push(
refs || [`refs/heads/${repoName || 'main'}:refs/heads/${repoName || 'main'}`],cloneOpts
).then(function() {
console.log('success',arguments);
});
});
}
}
module.exports = GitClient;
GithubClientRunner.js
var fs = require('fs');
var GitClient = require('./GithubClient.js');
var config = require('./github-config.js');
let repo;
console.log('starting clone');
return GitClient.clone({
branch: config.branch,remote: config.remote,local: config.local
}).then(r => {
repo = r;
let contents = new Date().getTime() + ' please ignore - will remove soon';
console.log('got repo,writing contents:',contents);
fs.writeFileSync(config.local + '/myTemporaryTestFile',contents);
// add the file
return GitClient.addAndCommit(repo,['myTemporaryTestFile'],'Test Commit not breaking anything');
}).then(() => {
console.log('commit done,pushing');
return GitClient.push(repo,config.branch);
})
.then(() => {
console.log('done');
process.exit(0);
})
.catch(err => {
console.error('uncaught!',err);
process.exit(1);
});
这是我自己的仓库的配置(工作):
const config = {
branch: 'main',remote: 'https://___MY_GITHUB_TOKEN___:x-oauth-basic@github.com/MyGitHubUserName/myTestRepo.git',local: './.gitcache/myTestRepo.io',username: 'MyGitHubUserName',realname: 'My Name',email: 'me@email.com',token: '___MY_GITHUB_TOKEN___'
};
module.exports = config;
结果如下:
starting clone
got repo,writing contents: 1612971904692 please ignore - will remove soon
commit done,pushing
success [Arguments] { '0': undefined }
done
现在这是组织存储库的配置(不工作):
const config = {
branch: 'main',remote: 'https://___MY_GITHUB_TOKEN___:x-oauth-basic@github.com/theOrganisationIAmAMemberOf/someOtherRepo.git',local: './.gitcache/someOtherRepo.io',token: '___MY_GITHUB_TOKEN___'
};
module.exports = config;
现在的结果是:
starting clone
[Error: unexpected HTTP status code: 403] {
errno: -1,errorFunction: 'Clone.clone'
}
如何使代码适用于两种类型的存储库?
谢谢!
解决方法
我自己发现了错误。代码按预期正常工作。 但是我忘了为组织启用我的令牌。一旦我这样做了,一切都很好。
专业提示:如果您遇到身份验证问题并且错误消息无助于尝试使用命令行 git 客户端实现相同的目标。错误消息会有更多信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。