How can I get Github access token for Github REST API?
Unanswered
Havana posted this in #help-forum
HavanaOP
I'm using App Router. How can I get Github API access token using next-auth? And is octokit the recommended way to fetch data from it?
15 Replies
HavanaOP
🙂
American
Its in the Developer Settings of your Github Profile -> https://github.com/settings/tokens
As it comes to fetching, its very user preference. Whatever works for you, could even just be a curl request.
@American Its in the Developer Settings of your Github Profile -> https://github.com/settings/tokens
HavanaOP
Sorry, not what I've meant. I want to send requests to API on behalf of the signed in user
Because it helps omit too strict rate limiting
American
Ah, it depends on how you do the authentication. I generally use NextAuth so I would grab the token from the session data:
import { getSession } from 'next-auth/react'
const SomeComponent = () => {
const { data: session } = getSession()
const githubAccessToken = session?.accessToken
}And then for instance with octokit:
const octokit = new Octokit({ auth: githubAccessToken })
// fetch repos
const repos = await octokit.rest.repos.listForAuthenticatedUser()HavanaOP
It doesnt contain accesstoken tho
American
Then its not included in the callbacks of the nextauth Github provider:
callbacks: {
async jwt(token, user, account, profile, isNewUser) {
// if access token exists, save token so it can be accessed later
if (account?.accessToken) {
token.accessToken = account.accessToken;
}
return token;
},
async session(session, token) {
// add access token to session
session.accessToken = token.accessToken;
return session;
}
},@American Then its not included in the callbacks of the nextauth Github provider:
` callbacks: {
async jwt(token, user, account, profile, isNewUser) {
// if access token exists, save token so it can be accessed later
if (account?.accessToken) {
token.accessToken = account.accessToken;
}
return token;
},
async session(session, token) {
// add access token to session
session.accessToken = token.accessToken;
return session;
}
},`
HavanaOP
that actually does nothing, accessToken will be just undefined
HavanaOP
Ok ive made it work
Is it safe to store them like this btw?
HavanaOP
