如何解决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 举报,一经查实,本站将立刻删除。