简体   繁体   English

通过PHP SDK登录Facebook,然后使用JS SDK

[英]Facebook Login via PHP SDK, then use JS SDK

I have developed an application where users login via facebook (PHP SDK); 我已经开发了一个应用程序,用户可以通过Facebook(PHP SDK)登录; now I would like to add JS SDK too, because I want to implement a generic friend selector ( http://facebook-friend-selector.codersgrave.com/ ) that works with JS SDK. 现在,我也想添加JS SDK,因为我想实现一个与JS SDK一起使用的通用好友选择器( http://facebook-friend-selector.codersgrave.com/ )。 I have two questions about: 我有两个问题:

1) Can mixing the two SDK lead to problems? 1)混合使用两个SDK会导致问题吗?

2) I added the JS SDK code as explained in the documentation and tried a simple request 2)我按照文档中的说明添加了JS SDK代码,并尝试了一个简单的请求

FB.api('/me/friends', function(response) {
   alert(JSON.stringify(response));
});

but I get an error (unknown error); 但是我得到一个错误(未知错误); I also tried a dialog box 我也尝试了一个对话框

FB.ui({
  method: 'feed',
  link: 'https://developers.facebook.com/docs/dialogs/',
  caption: 'An example caption',
}, function(response){});

but the box appears and suddenly disappears. 但是该框出现了,突然消失了。 I think the issue is related to the authentication...I guess, since my users are authenticated via PHP, that I need to pass the access token to JS, but how? 我认为问题与身份验证有关...我想,由于我的用户是通过PHP进行身份验证的,因此我需要将访问令牌传递给JS,但是怎么办? This question Passing the Facebook Authorization Token from PHP to Javascript seems to be exactly what I need but I don't understand the answer: it says to store the token in a cookie, but how can JS know about it, which name the cookie should have? 这个问题将Facebook授权令牌从PHP传递到Javascript似乎正是我所需要的,但我不明白答案:它说将令牌存储在cookie中,但是JS如何知道它,cookie应该使用哪个名称有? Finally, if I do something like 最后,如果我做类似的事情

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    alert('connected');
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;
  }
 });

I actually get a "connected" alert, that means that JS knows that the user is logged in, even if the login was via PHP, so where is the problem? 我实际上收到了“已连接”警报,这意味着JS知道用户已登录,即使该登录是通过PHP进行的,那么问题出在哪里呢?

Solved. 解决了。 My code contained two stupid mistakes, this is the correct code: 我的代码包含两个愚蠢的错误,这是正确的代码:

function test(){
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;

            FB.api('/me/friends', function(response) {
                alert(JSON.stringify(response));
            });
        }
    });
}

<a href="" onclick="test();return false;">Test</a>

In the first version the call to the friends method was outside the getLoginStatus method, so it was called before setting the variable accesToken, furthermore, I haven't the "return false" statement after the test() call. 在第一个版本中,对friends方法的调用在getLoginStatus方法之外,因此在设置变量accesToken之前调用了它,此外,在test()调用之后,我还没有“ return false”语句。

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

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