Next.js Discord

Discord Forum

Help Request: Retrieving Access Token for GitHub User Account

Unanswered
Polish Hound posted this in #help-forum
Open in Discord
Polish HoundOP
Hi there,

I'm writing to request assistance in retrieving the access token for a GitHub user account that has logged in and installed my app. I need this token to make changes to the user's account, such as creating repos and issues, through my API.

Could you please guide me on the necessary steps to obtain the access token? Any additional information or resources would be greatly appreciated.

here is some code example

import fs from "fs";
import axios from "axios";
import jwt from "jsonwebtoken";
import { Octokit } from "octokit";

export async function GET() {
  const privateKeyPath = "dupli-git.2024-05-06.private-key.pem";
  function getPrivateKey() {
    return fs.readFileSync(privateKeyPath, "utf8");
  }

  const privateKey = getPrivateKey();

  const token = jwt.sign({ iss: 885442 }, privateKey, {
    algorithm: "RS256",
    expiresIn: "10m",
  });

  const accessTokenResponse = await axios.post(
    `https://api.github.com/app/installations/51216376/access_tokens`,
    {},
    {
      headers: {
        Authorization: `Bearer ${token}`,
        Accept: "application/vnd.github.v3+json",
      },
    }
  );

  const octokit = new Octokit({
    auth: token,
  });
  console.log("ocatakit", token);
  const user = await octokit.request("GET /user/installations", {
    headers: {
      "X-GitHub-Api-Version": "2022-11-28",
    },
  });

  return Response.json({
    message: "Hello World",
    accessTokenResponse: accessTokenResponse.data,
    user: user.data,
  });
}


i need help at this point

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


this token is not valid all time how to generate this token also???

Thanks in advance for your support!

2 Replies

@ᴉuɐpɹɐɐ i got mine from this https://github.com/settings/tokens
Polish HoundOP
that's not the solution it's a way to access apis