Skip to content

Successive requests return RequestError [HttpError] [...] bad_verification_code #130

Open
@raimundo-henriques

Description

@raimundo-henriques

Hello everyone!

I am trying to run successive requests with one octokit instance and on the second request I'm getting this error:

RequestError [HttpError]: The code passed is incorrect or expired. (bad_verification_code, https://docs.github.com/apps/managing-oauth-apps/troubleshooting-oauth-app-access-token-request-errors/#bad-verification-code)

I'm using Next.js, passing the code from the frontend to an API route. Here's the code of the API route:

import type { NextApiRequest, NextApiResponse } from "next";
import { createOAuthUserAuth } from "@octokit/auth-oauth-user";
import { Octokit } from "@octokit/rest";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const { code } = req.body;

// Logs the expected code
  console.log("code", code);

  const octokit = new Octokit({
    authStrategy: createOAuthUserAuth,
    auth: {
      clientId: process.env.NEXT_PUBLIC_CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      code,
    },
  });

// First request
// Copied over from the documentation, at https://github.com/octokit/auth-oauth-user.js#usage-with-octokit
  const {
    data: { login },
  } = await octokit.rest.users.getAuthenticated();
// Logs my GitHub username, as expected
  console.log("Hello, %s!", login);

// Second request
// Throws an error
await octokit.rest.git.getRef({
    owner: "sine-fdn",
    repo: "tandem",
    ref: "heads/main",
  });

  res.status(200).json({ data: `${req.body}` });
}

I am new to octokit, so I am sorry if I'm missing something that should be clear.

Thank you very much for your work and for your attention!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: SupportAny questions, information, or general needs around the SDK or GitHub APIs

    Type

    No type

    Projects

    Status

    🔥 Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions