I'm using NextAuth's Google Provider for Google account sign-ins. I've successfully authenticated users and now I'd like to make API calls to YouTube's API for authenticated user details. I am not sure how to use the access_token or id_token from Google in my GET requests.
Here's an example of what my logs show in terms of the response I get back from Google when I sign in (personal details redacted for privacy):
[next-auth][debug][OAUTH_CALLBACK_RESPONSE] { profile: { id: '[REDACTED]', name: '[REDACTED]', email: '[REDACTED]', image: '[REDACTED]' }, account: { provider: 'google', type: 'oauth', providerAccountId: '[REDACTED]', access_token: '[REDACTED]', expires_at: [REDACTED], scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/youtube.readonly openid https://www.googleapis.com/auth/userinfo.email', token_type: 'Bearer', id_token: '[REDACTED]', }, OAuthProfile: { iss: 'https://accounts.google.com', azp: '[REDACTED]', aud: '[REDACTED]', sub: '[REDACTED]', email: '[REDACTED]', email_verified: true, at_hash: '[REDACTED]', name: '[REDACTED]', picture: '[REDACTED]', given_name: '[REDACTED]', locale: 'en', iat: [REDACTED], exp: [REDACTED] }}
So, I can literally see the access_token in this log data, but I don't know how to use it client-side to make calls to Youtube's api.
For additional context, this is my current Next Auth config:
import NextAuth from "next-auth";import GoogleProvider from "next-auth/providers/google";const options = { providers: [ GoogleProvider({ clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, authorization: { params: { scope: "openid email profile https://www.googleapis.com/auth/youtube.readonly", }, }, }), ], callbacks: { async jwt(token, user, account) { try { if (token.token.account?.access_token) { token.accessToken = token.token.account.access_token; } return token; } catch (error) { throw error; } }, async session(session, token) { try { if (token) { session.accessToken = token.accessToken; } return session; } catch (error) { throw error; } }, }, debug: true,};export default (req, res) => NextAuth(req, res, options);
I have tried to use this implementation to add the access_token
returned from Google to the NextAuth session data. I expected the session to have this information by default, but when I observed that it didn't I tried to adjust the callbacks in the Next Auth config shown above. I think I need this access token to fetch from YouTube's api as an authenticated user (per this youtube api documentation), but I haven't found a solution for adding this information to the session.
Is there a way to add it? Am I even supposed to have the access_token available in the session data?
Any help would be much appreciated. Thank you!