简体   繁体   English

使用Facebook API在PHP和JavaScript中使用Access_token

[英]Access_token in php and javascript with Facebook API

I am having a problem integrating my game into Facebook. 我在将游戏集成到Facebook时遇到问题。 Upon reading about the documentation, I read that the best and most up-to-date way to communicate with FB from Flash is via the Facebook API on javascript. 在阅读了文档之后,我读到,从Flash与FB进行通讯的最佳,最新方法是通过JavaScript上的Facebook API。 I call this to get all the ui dialogs and basic login information. 我称其为获取所有ui对话框和基本登录信息。 Now, my flash games connects to a php to send and receive highscores like so: 现在,我的Flash游戏连接到php,以发送和接收高分,如下所示:

scores.php?action=NEWSCORE&userId=123123123&score=100 scores.php?action = NEWSCORE&userId = 123123123&score = 100

scores.php?action=VIEW scores.php?action = VIEW

scores.php?action=VIEWFRIENDS&userId=123123123 scores.php?action = VIEWFRIENDS&userId = 123123123

Now, in scores.php, when I set the VIEWFRIENDS action, I want the php to create the facebook query. 现在,在scores.php中,当我设置VIEWFRIENDS操作时,我希望PHP创建Facebook查询。 I do this by adding 我通过添加

// Init the Facebook SDK
$facebook = new Facebook(array(
'appId'  => $app_id,
'secret' => $app_secret,
));

// Get the current user
$user = $facebook->getUser();

$access_token = $facebook->getAccessToken();
$fbresponse = $facebook->api("/me/friends?fields=installed", array('access_token'=>$access_token.$access_token,));

if ($fbresponse)
{
    $friendsData = $fbresponse['data'];
    $friendsString = '';
    $friendCount = 0;
    for ($i = 0; $i < sizeof($friendsData); $i++)
    {
        $friend = $friendsData[$i];

        if ($friend['installed'])
        {
            if ($friendCount > 0)
                $friendsString = $friendsString . ",";

            $friendsString = $friendsString . "'" . $friend['id'] . "'";                    $friendCount++;
        }
    }
    // Get the friends highscores
    $sql = "SELECT * FROM Highscores WHERE FacebookId IN (" . $friendsString . ") ORDER BY Score DESC LIMIT " . $scoresize ;
    $result = mysql_query($sql);
}

When doing this, all appears to work if I test it outside Facebook, but when I integrate it into my app, it throws the following error: 这样做时,如果我在Facebook之外对其进行测试,那么一切似乎都可以正常工作,但是当我将其集成到我的应用程序中时,它将引发以下错误:

 <br /> <b>Fatal error</b>: Uncaught OAuthException: Invalid access token signature. thrown in <b>/home/public_html/mysite.org/games/myGameFacebook/fb-php-sdk/base_facebook.php</b> 

on line 1106 1106

I am trying to check whats happening and I have yet to find out what to do about it. 我正在尝试检查正在发生的事情,但我还没有找到解决方法。 I understand the access token is generated on Javascript when logging in by this code. 我了解此代码登录时访问令牌是在Javascript上生成的。

getLoginStatus:function() {
    log("getLoginStatus");
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {

            log("Logged in.");
            F.token = response.authResponse.accessToken;
            F.getCurrentUserInfo();
        } else if (response.status === 'not_authorized') {
            log("User did not authorize app");
        } else {
            log("Not logged in.");
            FB.login(function(response) {
                if(response.session) {
                    log("Logging successful.");
                    F.token = response.session.access_token;
                    F.getCurrentUserInfo();
                } else {
                    log("Logging failed.");
                }
            });
        }
    });
},

How can I resolve this issue? 我该如何解决这个问题? I've been trying to understand how to do it, but I've read its either impossible, or link to some unknown session variable that has never worked. 我一直在试图了解如何做到这一点,但我已经读过它要么是不可能的,要么是链接到从未使用过的未知会话变量。

You're concatenating the access token with itself in the call to $facebook->api 您在调用$ facebook-> api时将访问令牌与其自身连接在一起

array('access_token'=>$access_token.$access_token,)

should look like: 应该看起来像:

array('access_token'=>$access_token,)

But I guess you should use an app access token to retrieve what you need, with a function like: 但是我想您应该使用应用访问令牌来检索所需内容,并具有以下功能:

 // Helper function to get an APP ACCESS TOKEN
 function get_app_access_token($app_id, $app_secret) {
   $token_url = 'https://graph.facebook.com/oauth/access_token?'
     . 'client_id=' . $app_id
     . '&client_secret=' . $app_secret
     . '&grant_type=client_credentials';

   $token_response =file_get_contents($token_url);
   $params = null;
   parse_str($token_response, $params);
   return  $params['access_token'];
 }

You'll need to pass it the app secret that you can read on Facebook Developer App Page. 您需要向其传递您可以在Facebook Developer App Page上阅读的应用秘密。

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

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