JSONP 回调似乎不起作用

问题描述

我一直在云函数中使用适用于 Nodejs 的 Firebase Admin SDK,以使用 Spotify 和 Firebase 身份验证创建自定义身份验证令牌。

我一直在尝试使用 Google 给出的示例,如下所示:

exports.token = functions.https.onRequest((req,res) => {
  try {
    cookieParser()(req,res,() => {
      functions.logger.log('Received verification state:',req.cookies.state);
      functions.logger.log('Received state:',req.query.state);
      if (!req.cookies.state) {
        throw new Error('State cookie not set or expired. Maybe you took too long to authorize. Please try again.');
      } else if (req.cookies.state !== req.query.state) {
        throw new Error('State validation Failed');
      }
      functions.logger.log('Received auth code:',req.query.code);
      Spotify.authorizationCodeGrant(req.query.code,(error,data) => {
        if (error) {
          throw error;
        }
        functions.logger.log(
          'Received Access Token:',data.body['access_token']
        );
        Spotify.setAccesstoken(data.body['access_token']);

        Spotify.getMe(async (error,userResults) => {
          if (error) {
            throw error;
          }
          functions.logger.log(
            'Auth code exchange result received:',userResults
          );
          // We have a Spotify access token and the user identity Now.
          const accesstoken = data.body['access_token'];
          const spotifyUserID = userResults.body['id'];
          const profilePic = userResults.body['images'][0]['url'];
          const userName = userResults.body['display_name'];
          const email = userResults.body['email'];

          // Create a Firebase account and get the Custom Auth Token.
          const firebasetoken = await createFirebaseAccount(spotifyUserID,userName,profilePic,email,accesstoken);
          // Serve an HTML page that signs the user in and updates the user profile.
          res.jsonp({token: firebasetoken});
        });
      });
    });
  } catch (error) {
    res.jsonp({error: error.toString()});
  }
  return null;
}); 

这是客户端发出请求的代码

const loginError = ref(null)
const route = useRoute()
console.log(route.query)
const { code,state,error } = route.query

function tokenReceived(data) {
      if (data.token) {
        projectAuth.signInWithCustomToken(data.token).then((userCredential) => {
          console.log(userCredential)
        })
      } else {
        console.error(data)
        document.body.innerText = 'Error in the token Function: ' + data.error
      }
}

if (error) {
      loginError.value = 'Error back from the Spotify auth page: ' + error
      } else if (code) {
      // Use JSONP to load the 'token' Firebase Function to exchange the auth code against a Firebase custom token.
      const script = document.createElement('script')
      script.type = 'text/javascript'
      // This is the URL to the HTTP triggered 'token' Firebase Function.
      // See https://firebase.google.com/docs/functions.
      const tokenFunctionURL =
        'http://localhost:5001/pacelist-main/us-central1/token'
      script.src =
        tokenFunctionURL +
        '?code=' +
        encodeURIComponent(code) +
        '&state=' +
        encodeURIComponent(state) +
        '&callback=' +
        tokenReceived.name
      document.head.appendChild(script)
} 

    
const signIn = () => {
      // Start the auth flow.
      window.location.href =
        'http://localhost:5001/pacelist-main/us-central1/redirect'
}

return { loginError,signIn }

此处为完整存储库:https://github.com/firebase/functions-samples/tree/main/spotify-auth

但是 jsonp 回调在返回到我的站点时似乎没有运行。它应该“提供一个用户登录并更新用户配置文件的 HTML 页面”。并登录用户,但它什么也不做。被这个问题困了好几天...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...