2013-04-12 20 views
5

Dựa trên facebook instructions (Scenario 4) tôi đang sử dụng URL saufacebook access token hết hạn

https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=CLIENT_SECRET& grant_type=fb_exchange_token&fb_exchange_token=OLD_ACCESS_TOKEN 

để có được truy cập mới thẻ nhưng tôi nhận được như sau:

{ "lỗi": { "tin nhắn ":" Lỗi khi xác thực mã thông báo truy cập: Phiên đã hết hạn vào thời gian unix 1365257820. Thời gian unix hiện tại là 1365759029. ", " loại ":" OAuthException ", " mã ": 190, " error_subcode ": 463}}

không hoạt động. Bất kỳ trợ giúp nào được đánh giá cao.

EDIT: OK! Làm việc như thế này

nếu access token hết hạn chạy script php dưới đầu tiên trên trình duyệt sau khi bạn lưu trữ nó trên máy chủ của bạn

<?php 
    $app_id = "your app id"; 
    $app_secret = "your app secret"; 
    $my_url = "http://apps.facebook.com/your_app_name"; 

    // known valid access token stored in a database 
    $access_token = "your old access token"; 

    $code = $_REQUEST["code"]; 

    // If we get a code, it means that we have re-authed the user 
    //and can get a valid access_token. 
    if (isset($code)) { 
    $token_url="https://graph.facebook.com/oauth/access_token?client_id=" 
     . $app_id . "&redirect_uri=" . urlencode($my_url) 
     . "&client_secret=" . $app_secret 
     . "&code=" . $code . "&display=popup"; 
    $response = file_get_contents($token_url); 
    $params = null; 
    parse_str($response, $params); 
    $access_token = $params['access_token']; 
    } 


    // Attempt to query the graph: 
    $graph_url = "https://graph.facebook.com/me?" 
    . "access_token=" . $access_token; 
    $response = curl_get_file_contents($graph_url); 
    $decoded_response = json_decode($response); 

    //Check for errors 
    if ($decoded_response->error) { 
    // check to see if this is an oAuth error: 
    if ($decoded_response->error->type== "OAuthException") { 
     // Retrieving a valid access token. 
     $dialog_url= "https://www.facebook.com/dialog/oauth?" 
     . "client_id=" . $app_id 
     . "&redirect_uri=" . urlencode($my_url); 
     echo("<script> top.location.href='" . $dialog_url 
     . "'</script>"); 
    } 
    else { 
     echo "other error has happened"; 
    } 
    } 
    else { 
    // success 
    echo("success" . $decoded_response->name); 
    echo($access_token); 
    } 

    // note this wrapper function exists in order to circumvent PHP’s 
    //strict obeying of HTTP error codes. In this case, Facebook 
    //returns error code 400 which PHP obeys and wipes out 
    //the response. 
    function curl_get_file_contents($URL) { 
    $c = curl_init(); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($c, CURLOPT_URL, $URL); 
    $contents = curl_exec($c); 
    $err = curl_getinfo($c,CURLINFO_HTTP_CODE); 
    curl_close($c); 
    if ($contents) return $contents; 
    else return FALSE; 
    } 
?> 

kịch bản trên sẽ cung cấp cho bạn một URL như hình dưới đây vào trình duyệt

https://graph.facebook.com/oauth/access_token?code= ...

sau đó nhận được chuỗi (nên được một cái gì đó như thế này: AQCn41Svv5DbWrnFY0Wf ..... YbNm_yz2rE # _) sau khi mã = ​​và dán nó vào mã = ​​URL dưới đây và CHẠY URL dưới đây vào trình duyệt

https://graph.facebook.com/oauth/access_token?client_id=App_Id&redirect_uri=http://apps.facebook.com/poemsoflove&client_secret=App_Secret&code=AQCn41Svv5DbWrnFY0Wf.....YbNm_yz2rE#_&display=popup

bạn sẽ nhận được phản ứng sau đó là một mã thông báo truy cập mới cho 60 ngày

access_token=<Extended_Access_Token>&expires=5180130 

sao chép và dán chuỗi sau access_token = vào tập lệnh trên máy chủ của bạn xuất bản các bài đăng mới trên trang của bạn

+1

Điều này có nghĩa rằng thẻ truy cập cũ của bạn đã hết hạn. Bạn có thể thử lại điều tương tự với mã thông báo truy cập ngắn được sống mới –

+0

cảm ơn tôi đã thực hiện nó bằng mã thông báo truy cập ngắn mới – stefanosn

+3

Bạn vừa viết mã thông báo truy cập cho thế giới, hãy cẩn thận với nó. Bất cứ ai cũng có thể lạm dụng nó. –

Trả lời

0
  1. Tạo ứng dụng Facebook Developer Page

Sau với php có thể truy cập trực tiếp Toke

$app_id = '{Application Id}'; 
$app_secret = '{Application Secret}'; 
$access_token = "https://graph.facebook.com/oauth/access_token?client_id=$app_id&client_secret=$app_secret&grant_type=client_credentials"; 
$access_token = file_get_contents($access_token); // returns 'accesstoken=APP_TOKEN|APP_SECRET' 
$access_token = str_replace('access_token=', '', $access_token); 
0
$app_id = 'APP_ID'; 
$app_secret = 'APP_SECRET'; 
$access_token_url = "https://graph.facebook.com/oauth/access_token?client_id=$app_id&client_secret=$app_secret&grant_type=client_credentials"; 
$access_token_data = file_get_contents($access_token_url); 
$access_token_arr = json_decode($access_token_data); 
$access_token = $access_token_arr->access_token; 
+1

Vui lòng không chỉ đăng một số mã làm câu trả lời. Vui lòng giải thích logic của bạn. –