简体   繁体   English

使用AJAX进行Facebook登录以获取访问令牌

[英]Facebook login using AJAX to get access token

I'm working on a facebook app that will allow users to login and will retrieve there photos. 我正在开发一个Facebook应用程序,该应用程序将允许用户登录并在其中检索照片。 I've got Facebook Login to work with a redirect to get the access token, but I'd actually like to make my app a little bit better and use ajax to get the access token. 我已经使用Facebook Login进行重定向以获取访问令牌,但实际上我想使我的应用程序更好一点,并使用Ajax来获取访问令牌。

I'm using the Facebook JS SDK to login into my app. 我正在使用Facebook JS SDK登录我的应用程序。 I'm using the Facebook PHP SDK for the rest of it, like retrieving user albums and photos. 我将使用Facebook PHP SDK的其余部分,例如检索用户相册和照片。

I'm confused about getting the access code using ajax from the login.php page. 我对从login.php页面使用ajax获取访问代码感到困惑。 I see that that page sets the access token and can return that value from this page. 我看到该页面设置了访问令牌,并且可以从该页面返回该值。

My loginWithFacebook function looks like this: 我的loginWithFacebook函数如下所示:

// REQUIRED - using the facebook js to login
  logInWithFacebook = function() {
    FB.login(function(response) {
      if (response.authResponse) {
        // alert('You are logged in & cookie set!');
        // Now you can redirect the user or do an AJAX request to
        // a PHP script that grabs the signed request from the cookie.

        // Redirect Way
        window.location.href = 'app5.1.js-login.php';


      } else {
        alert('User cancelled login or did not fully authorize.');
      }



    }, {scope: 'public_profile,email,user_photos'}
        );
    return false;
  };

As you can see I'm doing a redirect. 如您所见,我正在重定向。 I wanna change that part to pull in the access token with ajax, but I'm not exactly how to do that part and also how to keep it communicating with the rest of the app. 我想更改该部分以使用ajax插入访问令牌,但我不完全是如何执行该部分以及如何使其与应用程序其余部分保持通信。

Access tokens have been decoupled from SDK v.4.0. 访问令牌已从SDK v.4.0解耦。 Check out the following link: 查看以下链接:

https://www.sammyk.me/access-token-handling-best-practices-in-facebook-php-sdk-v4 https://www.sammyk.me/access-token-handling-best-practices-in-facebook-php-sdk-v4

Also check out: 还要签出:

https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM