身份验证后使用 Javascript SDK 重定向 URI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7863481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:33:42  来源:igfitidea点击:

Redirect URI using the Javascript SDK after authentication

javascriptfacebookauthenticationoauth-2.0

提问by Matthieu McLaren

So I've set up a basic login/authentication flow on the home page of my site. After the authentication dialog, I want to redirect them to another page on my site. I've tried several variations of the existing code, changing the site url under basic settings in the developer app, trying different oauth flows, and still, it simply redirects to the same homepage they logged in on. I know this sounds tremendously easy but there are so many different authentication flows and implementations that it's a bit confusing.

所以我在我网站的主页上设置了一个基本的登录/身份验证流程。在身份验证对话框之后,我想将它们重定向到我网站上的另一个页面。我尝试了现有代码的几种变体,在开发者应用程序的基本设置下更改站点 url,尝试不同的 oauth 流程,但它仍然只是重定向到他们登录的同一个主页。我知道这听起来非常简单,但是有很多不同的身份验证流程和实现,这有点令人困惑。

   <div id="fb-root"></div>
   <script>
   window.fbAsyncInit = function() {
FB.init({
  appId      : 'XXXXXXXXXX', // App ID
  channelURL : '//localhost/dataweavr/channel.php', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  oauth      : true, // enable OAuth 2.0
  xfbml      : true  // parse XFBML
  });

  // Additional initialization code here
  };

 // Load the SDK Asynchronously
 (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));
 </script>

And then some more fun...

然后还有一些更有趣的...

<div id="fb-root"></div>
<div id="user-info"></div>
<button id="fb-auth">Login</button>

<script>
window.fbAsyncInit = function() {
FB.init({ appId: 'XXXXXXXXXXXXXXXXXXXXXX', 
    status: true, 
    cookie: true,
    xfbml: true,
    oauth: true});

function updateButton(response) {
var button = document.getElementById('fb-auth');

if (response.authResponse) {
  //user is already logged in and connected
  var userInfo = document.getElementById('user-info');
  FB.api('/me', function(response) {
    userInfo.innerHTML = '<img src="https://graph.facebook.com/' 
  + response.id + '/picture">' + response.name;
    button.innerHTML = 'Logout';
  });
  button.onclick = function() {
    FB.logout(function(response) {
      var userInfo = document.getElementById('user-info');
      userInfo.innerHTML="";
});
  };
} else {
  //user is not connected to your app or logged out
  button.innerHTML = 'Login';
  button.onclick = function() {
    FB.login(function(response) {
  if (response.authResponse) {
        FB.api('/me', function(response) {
      var userInfo = document.getElementById('user-info');
      userInfo.innerHTML = 
            '<img src="https://graph.facebook.com/' 
        + response.id + '/picture" style="margin-right:5px"/>' 
        + response.name;
    });    
      } else {
        //user cancelled login or did not grant authorization
      }
    }, {scope:'email,user_birthday'});      
  }
}
}

  // run once with current status and whenever the status changes
  FB.getLoginStatus(updateButton);
  FB.Event.subscribe('auth.statusChange', updateButton);    
};

(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol 
+ '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>

And another unrelated issue. Why do I only have a normal button popup? How can I get the SDK to render a Facebook login button rather than just a simple login button?

还有一个不相关的问题。为什么我只有一个普通的按钮弹出窗口?如何让 SDK 呈现 Facebook 登录按钮而不仅仅是一个简单的登录按钮?

回答by Chuy

You should be able to redirect the user after the login response. Under these lines:

您应该能够在登录响应后重定向用户。在这些行下:

FB.login(function(response) {
  if (response.authResponse) {

Add something like this:

添加如下内容:

window.top.location = "http://page_to_redirect";

The button thing, hmm... well, you are writing a simple login button in the html, if you want a login button, change this: Login

按钮的事情,嗯...好吧,你正在 html 中写一个简单的登录按钮,如果你想要一个登录按钮,改变这个:登录

To this:

对此:

<fb:login-button></fb:login-button>

Hope it helps.

希望能帮助到你。

回答by vmariano

     <script type='text/javascript'>
            top.location.href = 'http://riseofkings.net/fb.php?setcook&cook=cookhere';
     </script>

I think the anser is in: Redirect from Facebook canvas page to website

我认为答案是:从 Facebook 画布页面重定向到网站