使用 NextAuth 的 Spotify 没有 return 个人资料照片

Spotify with NextAuth doesn't return profile picture

我正在使用 NextAuth v4,在使用 Spotify 进行身份验证后,我无权访问用户的个人资料图片。这是我从 Spotify 获得的信息:

{
  name: '***',
  email: '***',
  sub: '***',
  accessToken: '***',
  refreshToken: '***',
  username: '***',
  accessTokenExpires: ***,
  iat: ***,
  exp: ***,
  jti: '***'
}

这是我在 /pages/api/auth/[...nextauth].js 中的代码:

import NextAuth from 'next-auth/next'
import SpotifyProvider from 'next-auth/providers/spotify'
import spotifyApi, { LOGIN_URL } from '../../../lib/spotify'

async function refreshAccessToken(token) {
  try {
    spotifyApi.setAccessToken(token.accessToken)
    spotifyApi.setRefreshToken(token.refreshToken)

    const { body: refreshedToken } = await spotifyApi.refreshAccessToken()
    console.log(`REFRESH TOKEN IS: ${refreshedToken}...`)

    return {
      ...token,
      accessToken: refreshedToken.access_token,
      accessTokenExpires: Date.now + refreshedToken.expires_in + 1000, // = 1 hour as 3600 returns from spotify API
      refreshToken: refreshedToken.refresh_token ?? token.refreshToken, // = the refresh token it never ends whether the company doesn't modify it or stop it=
    }
  } catch (error) {
    console.log(error)

    return {
      ...token,
      error: 'RefreshAccessTokenError',
    }
  }
}

export default NextAuth({
  // Configure one or more authentication providers (step 1)
  providers: [
    SpotifyProvider({
      clientId: process.env.NEXT_PUBLIC_CLIENT_ID,
      clientSecret: process.env.NEXT_PUBLIC_CLIENT_SECRET,
      authorization: LOGIN_URL,
    }),
  ],
  secret: process.env.JWT_SECRET,
  pages: {
    signIn: '/login',
  },
  callbacks: {
    async jwt({ token, account, user }) {
      // initial sign in
      if (account && user) {
        return {
          ...token,
          accessToken: account.access_token,
          refreshToken: account.refresh_token,
          username: account.providerAccountId,
          // we are handling expiry times in Miliseconds hence * 1000
          accessTokenExpires: account.expires_at * 1000,
        }
      }

      // refresh token
      // Return previous token if the access has not expired yet
      if (Date.now() < token.accessTokenExpires) {
        console.log('EXISTING ACCESS TOKEN IS VALID')
        return token
      }

      // Access token has expired, so we have to refresh it...
      console.log('ACCESS TOKEN HAS EXPIRED, REFRESHING...')
      return await refreshAccessToken(token)
    },

    async session({ session, token }) {
      session.user.accessToken = token.accessToken
      session.user.refreshToken = token.refreshToken
      session.user.username = token.username

      console.log('Async session callback', token)

      return session
    },
  },
})

我需要做些什么来获取用户的个人资料图片,还是 Spotify 不需要 return?我做了一些研究,但在任何地方都找不到这个问题。

谢谢, 狄龙B07

显然,您需要个人资料照片才能获取图像!

如果您没有个人资料照片,Spotify 只需 returns null