Question

I have facebook application where I have used php CURL for authorization and to get access token and facebook user id. I have following code for allowing user access:

function AlowUserAccess()
 {
    include "config.php"; 
    error_reporting(0);
    if($_REQUEST['code'])
    {
        $returnurl = $fbconfig['baseUrl'];
        $url = "https://graph.facebook.com/oauth/access_token?client_id=".$fbconfig['appid']."&client_secret=".$fbconfig['secret']."&grant_type=fb_exchange_token&fb_exchange_token=".$_REQUEST['code'];
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_VERBOSE => true
            ));
        $result = curl_exec($ch);
        curl_close($ch);
            return $result;
}

I have print the return value $result of above function and return following errors:

 {"error":{"message":"Malformed access token AQxxxxxxxxxGypoLOAjcig9qSai_MuNna8marHxuddddCno6T_NqqD19zzzzzzzGNy_FJbcK9heZKPCSysXBYHFTD_AgzMxwE5BqIEHvyK-QGl5kv1R0bPOUx7V0xs5-OQdsfddsRc0uoR_y1ElTlY4YCrWr5dfssdfsdfdsITrXezhcv1S_pu-3g0Kx8v-VtsvMI6oVYlC0J4eogSL6yhIsWrNS6uuTZIWwE0BGWJCjVieU-BbCPLsqkasdVFjnPz489xdb2_bSfVBKvsdfsdfaefYtn3luygCs","type":"OAuthException","code":190}}
Était-ce utile?

La solution

You're using the code that Facebook sent you to try to get a long lived access token.

You must get a short lived access token first. Then exchange the short lived access token for the long lived access token (which is the call you are trying to make above).

Getting a short-lived access token:

https://graph.facebook.com/oauth/access_token?client_id={client_id}&redirect_uri={your redirect uri}&client_secret={client_secret}&code={code}

Where the code is the code you are currently using. Not sure if redirect_uri is required, but I use it.

This will return a form encoded string like

access_token=AQADEADBEEF&expires=11111

You can then use this access token (AQADEADBEEF) in place of your fb_exchange_token above

Autres conseils

Try the options like this:

CURLOPT_USERAGENT       => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
CURLOPT_COOKIEFILE      => 'anc.tmp',
CURLOPT_COOKIEJAR       => 'anc.tmp',
CURLOPT_FOLLOWLOCATION  => 1,
CURLOPT_RETURNTRANSFER  => 1,
CURLOPT_SSL_VERIFYHOST  => 0,
CURLOPT_SSL_VERIFYPEER  => 0,
CURLOPT_TIMEOUT         => 120

Google: SEM Labs CURL class for a wrapper.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top