Search code examples
phpfacebookauthenticationfbconnectaccess-token

facebook access_token request return NOTHING


it's been 5 days that I'm stuck in the implementation of a simple fb login and I'm banging my head like a motherf***r. anyways.

I'm at the point that I'm trying to set the accessToken by hand. like shown in this post.

https://github.com/facebook/php-sdk/issues/418#issuecomment-2605012

the only problem is that the oauth/access_token call returns nothing and I'm guessing that it's a pretty messed up behaviour. this is the snippet I use to make the call

                $token_url = "https://graph.facebook.com/oauth/access_token?client_id=".FB_APPID."&redirect_uri=".urlencode($curUrl)."&client_secret=".FB_APPSECRET."&code=".$_GET['code'];
                log_to_file("curPageURL: ".$token_url);
                $response = file_get_contents($token_url);
                log_to_file("resp: ".$respone);

the log_to_file is a custm method that logs the taken message in a file so that I can have the log situation in a tail -f scenario.

what happens here is that the log "resp: ". returns nothing at all.

did anyone face the same problem?

thx in advance. this thing is driving me insane.

and I can officially state that the fb sdk is the most buggy and worse-documented service I've ever used.


Solution

  • Facebook has the most badly-documented API in the world. I remember the time I used it and couldn't help swearing all the time ! :) Here is a piece of pseudo-code that did work for me. It gains permission from a user to post a link on his wall. I'm just posting it so that maybe you can take some hints and make your code work:

    <?php
    
    //A function for cURL operations.
    function callFb($url)
    {
                $ch = curl_init();
                curl_setopt_array($ch, array(
                    CURLOPT_URL => $url,
                    CURLOPT_RETURNTRANSFER => true
                ));
    
                $result = curl_exec($ch);
                curl_close($ch);
                return $result;
    }
    
    $url = "https://graph.facebook.com/oauth/access_token?client_id=<your_client_id>&redirect_uri=".urlencode("<the_url_where_the_user_is_redirected_after_granting_permission>")."&client_secret=<your_client_secret>";
    
    /* Get access token. */
    $access_token = callFb($url);
    
    /* Parse the result to get access token */
    $access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));
    
    /* Save access token, if you want to for future.*/
    mysql_query("INSERT INTO fb_auth_tokens (id,auth_token) VALUES('$_GET[id]','$auth_token')");
    
    /* Post to users wall */
    
    $apprequest_url = "https://graph.facebook.com/me/feed";
    $mymessage="Hello World !";
    $parameters = "?access_token=" . $access_token . "&message=" .
    urlencode($mymessage) .
            "&link=".urlencode("<link_that_you_want_to_post>").
            "&description=<description_of_the_link>".
            "&method=post";
    $myurl = $apprequest_url . $parameters;
    $result = callFb($myurl);
    
    // Thy shall be done. :)