php - Error trying to get accesstoken

140

I need to create a system that when the cliente publish something in his website it will be published in his facebook, but to do this i need to get the access token because the account need to still logged to my system.

I made my app and tested it in my own facebook and when i try to login by my system it work, but when i try to login with the system in the client facebook, return this error:

An error has occurred. Try again later.

My code to generate the AccessToken is this:

require_once("php-sdk/src/facebook.php"); //Up-to-date SDK files from Git

    $app_id = "APP_ID"; //ID do APP
    $app_secret = "APP_SECRET"; //Secret do APP

    $facebook = new Facebook(array(
      'appId'  => $app_id,
      'secret' => $app_secret
    ));

    $page_id = 'PAGE_ID';
    $message = "Teste de integração";

    $token_url = 'https://graph.facebook.com/oauth/access_token?'
        . 'client_id=' . $app_id
        . '&client_secret=' . $app_secret
        . '&grant_type=client_credentials';

    $token_response = file_get_contents_curl($token_url);
    $params = null;
    parse_str($token_response, $params);
    $app_access_token = $params['access_token'];

    // Get User ID
    $user = $facebook->getUser();

    if ($user) {
      try {
        $page_info = $facebook->api("/$page_id/?fields=access_token");
        //print_r($page_info);

        if( !empty($page_info['access_token']) ) {
        //if(!empty($app_access_token)) {
            $args = array(
                'access_token'  => $page_info['access_token'],
                'message'       => $message
            );
           // print_r($args);
           // $post_id = $facebook->api("/$page_id/feed","post",$args);
        } else {
            $permissions = $facebook->api("/me/permissions");
            if( !array_key_exists('publish_stream', $permissions['data'][0]) ||
               !array_key_exists('manage_pages', $permissions['data'][0])) {
                    // We don't have one of the permissions
                    // Alert the admin or ask for the permission!
                    header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages")) );
            }
        }
      } 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'=>'manage_pages,publish_stream, offline_access'));
    }

?>

<?php if (!$user): ?>
    <a href="<?php echo $loginUrl ?>">Login with Facebook</a>
<?php endif ?>

I already try to reconfig the user and all the config from the user is the same from the another account that didnt return the error.

People are also looking for solutions to the problem: javascript - How to open a pop-up window based on selected id using radio button and how to pass that selected id to pop-up window, using php,MySQL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.