#2842·octokit.js

[BUG]:无法删除(撤销)或刷新用户令牌

作者: Debajyati创建于 2025年5月6日更新于 2025年5月7日
标签Type: BugStatus: Triage

Recently, today I was using my app and tried to logout from my app (It is done by revoking the user token and then clearing the revoked token from the local config file). It didn't work and threw an error. The function (when called) which used to revoke the token uses this code -

javascript
await octokit.rest.apps.deleteToken({
  client_id: config.CLIENT_ID,
  access_token: storedToken,
});

This is the code of the complete function -

javascript
const revokeToken = async () => {
  try {
    const { default:input } = await import("../utils/input.js");
    const { default:chalk } = await import("chalk");
    const yesOrNo = await input(chalk.red("Are you sure you want to revoke the token? [y/N]"));
    if (yesOrNo.toLowerCase() !== "y") {
      console.log("Token revocation aborted.");
      return;
    }
    const { Octokit } = await import("@octokit/rest");
    const tokenFilePath = config.TOKEN_FILE;
    const storedToken = getStoredToken(tokenFilePath);

    if (!storedToken) {
      console.error("Error: Token not found.");
      console.error("You are not authenticated.");
      return;
    }

    const isValid = await checkTokenValidity(storedToken);
    if (!isValid) {
      console.log("Token is already expired. No need to revoke.");
      clearToken(tokenFilePath);
      return;
    }

    const authType = getStoredAuthType(tokenFilePath);
    if (authType !== "oauth") {
      console.log("Token type is not OAuth. Skipping revocation.");
      return;
    }

    const octokit = new Octokit({ auth: storedToken });

    // Attempt to revoke the token
    await octokit.rest.apps.deleteToken({
      client_id: config.CLIENT_ID,
      access_token: storedToken,
    });
    console.log("Token revoked successfully!");

    clearToken(tokenFilePath);
  } catch (error) {
    console.error("An error occurred while revoking the token:", error.message);
    process.exit(1);
  }
};

Previously it used to execute successfully without any error. Now, this throws error (image below) - The Error occurs when trying to revoke the token

内容来源: octokit/octokit.js