一旦用户授权我的应用程序,我需要解析签名的请求,以便我可以访问他们的用户 ID 等。在我的代码中,我将 url 放在处理它的页面的哪个位置?我需要一个代码示例。我查看了文档,但没有清楚地解释如何为画布应用程序执行此操作。变量 redirectUrl 具有画布应用程序本身的 url。那是否应该包含解析签名请求的代码的url?我不确定。
<body>
<div id="fb-root"></div>
<script type="text/javascript">
$(document).ready(function(){
var appId = 278*****2040;
// If logging in as a Facebook canvas application use this URL.
var redirectUrl = "http://apps.facebook.com/MYAPP";
// If the user did not grant the app authorization go ahead and
// tell them that. Stop code execution.
if (0 <= window.location.href.indexOf ("error_reason"))
{
$('#authCancel').empty();
$(document.body).append ("<p>Authorization denied!</p>");
return;
}
// When the Facebook SDK script has finished loading init the
// SDK and then get the login status of the user. The status is
// reported in the handler.
window.fbAsyncInit = function(){
//debugger;
FB.init({
appId : 278****40,
status : true,
cookie : true,
oauth : true
});
// Sandbox Mode must be disabled in the application's settings
// otherwise the callback will never be invoked. Monitoring network
// traffic will show an error message in the response. Change the
// Sandbox Mode setting in: App Settings - Advanced - Authentication.
FB.getLoginStatus (onCheckLoginStatus);
};
// Loads the Facebook SDK script.
(function(d)
{
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
// Handles the response from getting the user's login status.
// If the user is logged in and the app is authorized go ahead
// and start running the application. If they are not logged in
// then redirect to the auth dialog.
function onCheckLoginStatus (response)
{
if (response.status != "connected")
{
top.location.href = "https://www.facebook.com/dialog/oauth?client_id=" + appId + "&redirect_uri=" + encodeURIComponent (redirectUrl) + "&scope=user_photos,friends_photos";
}
else
{
// Start the application (this is just demo code)!
$(document.body).append ("<p>Authorized!</p>");
FB.api('/me', function (response) {
$(document.body).append ("<pre>" + JSON.stringify (response, null, "\t") + "</pre>");
});
}
}
});
</script>