无法从 Github 帐户访问/删除秘密要点,即使具有正确的身份验证详细信息



我已经搜索了很多,但找不到任何有用的东西,而且我不是一个流利的Nodejs开发人员。

我正在尝试使用以下代码从我的 Github 帐户中删除我的旧和过时的秘密要点,但它只能正确执行身份验证部分。

#!/usr/bin/env node
const Octokit = require('@octokit/rest')
const octokit = new Octokit()
var async = require('async');
var github = new Octokit({
    version: '14.0.0',
    protocol: 'https'
});
github.authenticate({
    type: 'basic',
    username: '###############',
    password: '###############'
});
async.waterfall([
    function (callback) {
        console.log(github.gists.getAll());
        github.gists.getAll({}, callback);
    },
    function (gists, callback) {
        // filter gists by properties as needed
        async.each(gists, function (gist, callback) {
            github.gists.delete({
                id: gist.id
            }, callback);
        }, callback);
    }
], function (err) {
    if (err) {
        console.log('Execution failed: %s', err.message);
        process.exit(1);
    }
    console.log('Done!');
    process.exit(0);
});

当我在Gitbash(安装了Node和Npm的Windows 7 64Bit)中运行上述脚本时,它给出了此错误:

Promise { <pending> }
Execution failed: {"message":"Not Found","documentation_url":"https://developer.github.com/v3"}

但我知道那些秘密要点就在那里。

当我喜欢时,它甚至没有列出那些秘密要点,

console.log(gist.id)

在函数调用async之后。

任何帮助,不胜感激。

看起来您使用的是过时的@octokit/rest。确保您此时已安装最新版本 v16.3.1。您可以通过在终端中运行npm ls @octokit/rest来检查已安装的版本。

因为您可能会加载和删除很多要点,所以我建议使用油门插件。它将帮助您防止达到滥用/速率限制。这是完整的代码。

const Octokit = require('.')
  .plugin(require('@octokit/plugin-throttling'))
const octokit = new Octokit({
  auth: {
    username: '###############',
    password: '###############'
  },
  throttle: {
    onAbuseLimit: (retryAfter) => console.log(`abuse limit hit, retrying after ${retryAfter}s`),
    onRateLimit: (retryAfter) => console.log(`rate limit hit, retrying after ${retryAfter}s`)
  }
})
// load all your gists, 100 at a time.
octokit.paginate('/gists?per_page=100')
  .then((gists) => {
    // filter out the public gists
    const privateGists = gists.filter(gist => !gist.public)
    console.log(`${privateGists.length} gists found`)
    // delete all private gists
    return Promise.all(privateGists.map(gist => octokit.gists.delete({ gist_id: gist.id }))
  })
  .then(() => {
    console.log('done')
  })

我建议创建专用访问令牌,而不是使用您的用户名和密码进行身份验证。确保选择"要点"范围:https://github.com/settings/tokens/new?scopes=gist&description=deleting%20private%20gists

然后将令牌传递给构造函数的身份验证选项,而不是用户名和密码

const octokit = new Octokit({
  auth: 'token <your token here>',
  throttle: {
    onAbuseLimit: (retryAfter) => console.log(`abuse limit hit, retrying after ${retryAfter}s`),
    onRateLimit: (retryAfter) => console.log(`rate limit hit, retrying after ${retryAfter}s`)
  }
})

相关内容

最新更新