2011-08-18 58 views
2

我正在尝试使用离线标记登录用户到Facebook(我已将其工作了很长时间),但似乎Facebook可能会更改某些内容?Facebook API已更改

快速运行: 我有通过OAuth进行Facebook PHP SDK(2.1)身份验证。我记得获取访问令牌后的返回数据是这样的:

callbackurl?session={access_token:....,expires:....,session_key....} 

etc ..基本上是一个JSON对象的URL。

但现在看来,他们已经改变参数和所有我得到的回复是这样的:

[fb_199972360033499_access_token] What are these digits? 
[fb_199972360033499_user_id] 

(我与(PHP SDK 2.1和3.1.1)试图

以前我是存储与

$facebook->setSession($dataArray); 

设置保存令牌数据但现在我已经不知道该怎么办,如果任何人有哪里找标识欣赏它,因为我有一个很难找到的RI任何线索ght指令在实际的FB文档中。

回答

2

新的PHP SDK

有关使用新的PHP SDK中的例子,你应该看看:

https://github.com/facebook/php-sdk

http://developers.facebook.com/blog/post/534/

这也是一个不错的主意,采取一看源代码。

脱机访问

http://developers.facebook.com/docs/authentication/

如果你有一个授权用户现在就可以获取用户access_token有:

$access_token = $facebook->getAccessToken(); 

如果没有授权的用户可用它会退回申请访问令牌。

如果您的应用需要具有无限到期时间的访问令牌(可能在用户未使用您的应用后代表用户执行操作),则可以请求offline_access权限。

获得用户ID:

$user_id = $facebook->getUser(); 

应用程序ID

[fb_199972360033499_access_token]这些是什么数字?

这些数字应该是您的应用程序ID。

持久性数据

$ facebook-> setSession($ dataArray中);

这应该不再是必要的了,因为PHP SDK会照顾到这一点。除非你想存储自己的数据 - 在这种情况下,你应该创建自己的会话数据。

+0

谢谢!你和ifaour帮了大忙!看起来他们改变了一些东西,并修复了这个问题:) – JREAM

+0

非常欢迎。 – jBit

2

让我们用新PHP-SDK让你的访问令牌:

<?php 
/** 
* Copyright 2011 Facebook, Inc. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may 
* not use this file except in compliance with the License. You may obtain 
* a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
* License for the specific language governing permissions and limitations 
* under the License. 
*/ 

require './src/311/facebook.php'; 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => 'APP_ID', 
    'secret' => 'APP_SECRET', 
)); 

// Get User ID 
$user = $facebook->getUser(); 
// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    } 
} 

// Login or logout url will be needed depending on current user state. 
if ($user) { 
    $logoutUrl = $facebook->getLogoutUrl(); 
} else { 
    $loginUrl = $facebook->getLoginUrl(array('scope'=>'offline_access')); 
} 
?> 
<!doctype html> 
<html xmlns:fb="http://www.facebook.com/2008/fbml"> 
    <head> 
    <title>php-sdk</title> 
    <style> 
     body { 
     font-family: 'Lucida Grande', Verdana, Arial, sans-serif; 
     } 
     h1 a { 
     text-decoration: none; 
     color: #3b5998; 
     } 
     h1 a:hover { 
     text-decoration: underline; 
     } 
    </style> 
    </head> 
    <body> 
    <h1>php-sdk</h1> 

    <?php if ($user): ?> 
     <a href="<?php echo $logoutUrl; ?>">Logout</a> 
    <?php else: ?> 
     <div> 
     Login using OAuth 2.0 handled by the PHP SDK: 
     <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> 
     </div> 
    <?php endif ?> 

    <h3>PHP Session</h3> 
    <pre><?php print_r($_SESSION); ?></pre> 

    <?php if ($user): ?> 
     <h3>You</h3> 
     <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"> 

     <h3>Your User Object (/me)</h3> 
     <pre><?php print_r($user_profile); ?></pre> 
    <?php else: ?> 
     <strong><em>You are not Connected.</em></strong> 
    <?php endif ?> 
    </body> 
</html> 

这里有两点要注意:

  • 我们要求的offline_access许可构建登录网址
  • 一旦你登录,你会得到access_token(你说你已经有了它,所以继续阅读)

现在从Facebook注销,让我们使用access_token

<?php 
require './src/311/facebook.php'; 

$facebook = new Facebook(array(
    'appId' => 'APP_ID', 
    'secret' => 'APP_SECRET', 
)); 

$offline_access_token = "XXXXXX"; 
try { 
    $user_profile = $facebook->api('/me', 'GET', array('access_token'=>$offline_access_token)); 
    echo "<pre>"; 
    print_r($user_profile); 
    echo "</pre>"; 
} catch (FacebookApiException $e) { 
    error_log($e); 
} 
?> 
+0

谢谢你,你和jbit帮助我解决了这个问题! – JREAM