I am developing a Facebook canvas appliaction, and I am trying to do the following, which is standard in many applications.
- User arrives on my application page.
- If he has not granted the necessary permissions, he is redirected to a page where he can (optionally authenticate) and accept the permissions, and
- finally redirected back t开发者_Python百科o my facebook application page.
As far as I understand, this is achieved by redirecting the user to the OAuth dialog, on
http://www.facebook.com/dialog/oauth/?
scope=PERMISSIONS&
client_id=CLIENT_ID&
redirect_uri=MY_CANVAS_URL&
response_type=token
The problem is that Facebook will not allow a redirect from inside an iframe, for security reasons. It only allows redirects to what I declare as site URL
on the application settings page. In my case I have set that to the URL of the application on Facebook, since that is what I put as redirect_uri
(and it works).
So, instead of being able to see the authentication dialog, the user is presented only a link to it. A kind of workaround is to issue the redirect with javascript, by manually settings top.location
. But this is not a real solution, since part of the site already shows before Javascript redirects, and I get an annoying flicker.
What is the correct way to obtain this flow?
You need to use:
top.location.href=myAuthUrl;
There is a tutorial on canvas auth here: https://developers.facebook.com/docs/appsonfacebook/tutorial/#auth
you can use a validation page before you redirect to your app home page . something like this :
<html>
<head>
<script src="facebook_javascript_api_file_src"></script>
</head>
<body>
<div id="fb-root"></div>
<script>
function verify(){
FB.init({
appId:"app_id",
status:true,
cookie:true,
xfbml:true
}
);
FB.getLoginStatus(function(response){
if(response.session){
top.location.href="url to your app home page";
}
else{
top.location.href="https://www.facebook.com/dialog/
oauth?client_id=APP_ID&redirect_uri=redirect_url&scope=email,read_stream";
}
})
}
verify();
</script>
</body>
</html>
精彩评论