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

时间:2019-01-24 23:12:12

标签: node.js github node-modules async.js octokit

我为此进行了很多搜索,但找不到任何有用的东西,而且我不是流利的Node.js开发人员。

我正在尝试使用下面的代码从我的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函数调用之后。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

由于您可能要加载和删除许多要点,因此我建议使用throttle plugin。这将帮助您防止达到滥用/速率限制。这是完整的代码。

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

然后将令牌(而不是用户名和密码)传递给构造函数的auth选项

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`)
  }
})