2011-07-22 66 views
1

我在我的應用程序中使用請求對話框,用戶可以向其朋友發送請求。一旦用戶發送請求,我將應用程序重定向到它在用戶頁面上發佈的頁面。我用下面的代碼來獲得一個的access_token:Facebook access_token問題

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&redirect_uri=http://www.facebook.com/pages/Cosmetics/167231286678063?sk=app_233227910028874&grant_type=client_credentials'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); 

$token = curl_exec($ch); 

$me = $facebook->api('/me?'.$token); 

但是當我嘗試張貼在牆上它顯示了這個錯誤:

致命錯誤:未捕獲OAuthException:無效OAuth訪問令牌簽名。

回答

1

爲什麼不使用PHP SDK 3.0.1?

它很容易,如果你使用Facebook提供的SDK,這裏是使用PHP SDK 3.0.1認證樣本:

<?php 

// Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/ 
require ('facebook.php'); 

define('FACEBOOK_APP_ID',"YOUR-APP-ID-HERE"); 
define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET-HERE"); 
define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE"); 
define('PERMISSIONS_REQUIRED', "publish_stream,user_photos"); 
$user = null; 

$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID, 
    'secret' => FACEBOOK_SECRET, 
    'cookie' => true 
)); 

$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected. 

if($user == 0) { 
    // If the user is not connected to your application, redirect the user to authentication page 
    /** 
    * Get a Login URL for use with redirects. By default, full page redirect is 
    * assumed. If you are using the generated URL with a window.open() call in 
    * JavaScript, you can pass in display=popup as part of the $params. 
    * 
    * The parameters: 
    * - redirect_uri: the url to go to after a successful login 
    * - scope: comma separated list of requested extended perms 
    */ 

    $login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED)); 

    echo ("<script> top.location.href='".$login_url."'</script>"); 

} else { 
    // if the user is already connected, then fetch access_token and user's information or show some content to logged in user. 
    try 
    { 
     $access_token = $facebook->getAccessToken(); // Gives you current user's access_token 

     $user = $facebook->api('/me'); // Gets User's information based on permissions the user has granted to your application. 

    } catch(FacebookApiException $e){ 
     $results = $e->getResult(); 
     // Print results if you want to debug. 
    } 

} 

?> 
+0

非常感謝它讓我的生活更輕鬆 –

+0

沒問題隊友,很高興它幫助你:) –

1

您使用的是&grant_type=client_credentials,它爲您的應用程序提供整個訪問令牌,而不是您當前的用戶。要獲取當前用戶的訪問令牌,您需要將它們重定向到相同的url,但是沒有&grant_type=client_credentials,然後Facebook會將它們重定向回您的redirect_uri並沿您所需的access_token發送。或者,您可以使用JavaScript SDK通過彈出窗口獲取訪問令牌,而無需重定向它們。

這裏是另外一個問題一個鏈接,如何與JS SDK沒有重定向驗證:Facebook Authentication Without Redirect?

0

你在做什麼有獲得application access token

App Login allows you to take various administrative actions for your app, such as retrieving Insights data or approving Requests. Graph API calls that require an app access token are clearly denoted in the API reference.

你需要的是一個user access token與一些permissions,在你的情況下,它將是publish_stream

2

我不知道你試圖究竟是如何進行身份驗證。它可能是好的這樣的請求方式,但我使用的方法是一個記錄在這裏:http://developers.facebook.com/docs/authentication/

也就是說,你先將用戶重定向到Facebook頁面以使他接受權限。一旦他接受,他被重定向到您所提供的網址,你需要爲了得到您的access_token需要真正執行服務器端請求code=###獲取參數的..

重定向網址示例:

$my_url = 'http://www.example.com'; 
"http://www.facebook.com/dialog/oauth?client_id=". $app_id ."&redirect_uri=". urlencode($my_url); 

然後,當用戶點擊接受,他被重定向到,例如

http://www.example.com?code=ABCDEF 

然後,在服務器端,你需要得到製作的access_token這樣一個電話:

"https://graph.facebook.com/oauth/access_token?" 
    . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret . "&code=" . $code; 

然後仔細檢查從調用返回的文本,並嘗試獲得

"https://graph.facebook.com/me?access_token=". $access_token; 

(您也可以手動檢查)

編輯

此外,您可能需要來自用戶的更多權限,以便在他的流上發佈。所以,請求權限添加scope=publish_stream獲得第一個(用戶授權)URL。