简体   繁体   中英

Facebook Access Tokens

I am trying to get the Facebook access tokens useing the bellow code: I did create my own function before but it came back with the same error and i can't work out what is the problem.

function getAccessToken(){
        $app_id = FB_APP_ID;
        $app_secret = FB_SECRET_ID;
        $my_url = FB_PAGE_URL;
        $code = $_REQUEST["code"];   


        if(empty($code)) {
       $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
       $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
         . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
         . $_SESSION['state'];

       echo("<script> top.location.href='" . $dialog_url . "'</script>");
     }      
    if($_REQUEST['state'] == $_SESSION['state']) {
       $token_url = "https://graph.facebook.com/oauth/access_token?"
         . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
         . "&client_secret=" . $app_secret . "&code=" . $code;

       $response = file_get_contents($token_url);
       $params = null;
       parse_str($response, $params);

       $graph_url = "https://graph.facebook.com/me?access_token=" 
         . $params['access_token'];

       $user = json_decode(file_get_contents($graph_url));
       echo("Hello " . $user->name);
     }
     else {
       echo("The state does not match. You may be a victim of CSRF.");
     }
}

Taken from Server-Side Authentication but i keep getting this error:

    Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?
client_id=ID HERE&redirect_uri=URLHERE&cli
ent_secret=SECRECT&code=AQCI5rNgw9zCPHWGozeT59asg7_022u5tVc5XSef49BiX
IaF5_MAMqFwsqOAquUHgjOu_99ONwUV6IC7k-jV6DsWf9ni3jm8t59aHCBp1jrFaDthPbIKLNLQ-
fZgB5MLh1le5BAPKj_l57jhTLTBfOdxRU30mFCMYzMch8MYFpCmJ9GrjSSGwt0OKb_LNqMoRf8) [function.file-
get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

Anyone know the fix?

It might be a problem with the file_get_contents method, since the url you are constructing looks ok.

If you search for "facebook file_get_contents failed to open stream" you'll get quite a few results on the matter, a lot right here on stack overflow.
It looks like a HTTPS problem with the file_get_contents as states in this thread: [function.file-get-contents]: failed to open stream .

Maybe try some other method of issuing http requests? Maybe curl?
Just to test things, try to use curl from the command line with the same url you tried with the php script and see what you get.

Looking at the request response, you haven't defined your Facebook application settings.

You're using FB_APP_ID , FB_SECRET_ID and FB_PAGE_URL when you request a token, but in the response I can see these are defined as: "ID HERE", "SECRECT" and "URLHERE" respectively.

I have managed to fix this issue using the PHP SDK, and after a long battle with the SDK to get it to do what i wanted.

I did also open another question with the same idea but with another problem. This problem has now been solved.

Check out this question for the answer to this problem: Facebook Access Token Server Side Authentication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM