How do I renew a Facebook user_access_token if I deal with a lot of AJAX?

后端 未结 7 1082

Please tell me if I\'m understanding correctly. (because I might not be.)

  1. User posts something on my site. (He checked \"also post to Facebook\".)
  2. Client
7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-09 13:25

    I hope you known that if you have publish_stream permission you don't need access token here is documentation for publish_stream and Below is the solution for four scenarios

    1.The token expires after expires time (2 hours is the default).
    2.The user changes his/her password which invalidates the access token.
    3.The user de-authorizes your app.
    4.The user logs out of Facebook.

    To ensure the best experience for your users, your app needs to be prepared to catch errors for the above scenarios. The following PHP code shows you how to handle these errors and retrieve a new access token.

    When you redirect the user to the auth dialog, the user is not prompted for permissions if the user has already authorized your application. Facebook will return you a valid access token without any user facing dialog. However if the user has de-authorized your application then the user will need to re-authorize your application for you to get the access_token.

    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("");
    }
    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_SSL_VERIFYPEER, false);
    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;
    }
    ?>
    

    for more details info you can visit this link
    Thanks

提交回复
热议问题