2011-08-18 57 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