gpt4 book ai didi

javascript - 当 JS SDK 初始化并准备好进行 API 调用时的回调

转载 作者:行者123 更新时间:2023-11-30 06:54:40 24 4
gpt4 key购买 nike

我正在异步加载 JS SDK 并在 fbAsyncInit 回调中运行 FB.init。 FB.init 运行后,我想尽快进行 API 调用,但是在 FB.init 之后进行 API 调用时,我得到以下 OAuthException

Object { type="OAuthException", message="An active access token must be used to query information about the current user."}

如果我将 API 调用延迟 X 秒,它就会起作用。那么,如何在 SDK 准备好 API 调用时获得回调?

请参阅下面的代码示例。 FB.init 的参数“authResponse”由 PHP SDK (getSignedRequest()) 生成。

<!DOCTYPE html>
<html>

<button onclick="getMe();">Get me</button>

<script>
function getMe() {
FB.api('/me', function(response) {
console.log(response);
});
}
</script>

<div id="fb-root"></div>
<script style="text/javascript">
window.fbAsyncInit = function() {
console.log("fbAsyncInit called");
FB.init({appId: "XXX", status: true, cookie: false, authResponse: {"algorithm":"HMAC-SHA256","expires":1314802800,"issued_at":1314798072,"oauth_token":"XXX","user":{"country":"se","locale":"en_US","age":{"min":21}},"user_id":"XXX"}, oauth: true, xfbml: true});
FB.api("/me", function(response) {console.log(response);});
};
(function() {
var e = document.createElement("script"); e.async = true;
e.src = document.location.protocol + "//connect.facebook.net/en_GB/all.js";
document.getElementById("fb-root").appendChild(e);
}());
</script>
</body>
</html>

最佳答案

如果您在 FB.init 被调用后立即订阅 auth.statusChange 事件,我相信它会在您获得 session 时被调用,然后可以安全地使用 FB.apiSee the docsFB.Event.subscribe 上获取更多详细信息。

关于javascript - 当 JS SDK 初始化并准备好进行 API 调用时的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7258136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com