facebook通过curl auth失败了

时间:2017-03-29 11:41:49

标签: php facebook-graph-api curl

我通过curl使用FB授权。到现在为止半年一直都很好。但在过去的几天里,我发现了一条消息:

    [{"error":{"message":"This_authorization_code_has_been_used_","type":"OAuthException","code":100,"fbtrace_id":"HuUHKJv\/zGu"}}] =>
    $params = array(
        'client_id'     => FACEBOOK_ID,
        'redirect_uri'  => PATH_ROOT.REDIRECT_URI,
        'response_type' => 'code',
        'scope'         => 'email, user_birthday'
);
<a href="<?php echo FACEBOOK_OAUTH . '?' . urldecode(http_build_query($params))?>" class="facebook-icon-edit">Change</a>



callback:

if(isset($_GET['code']) and strlen($_GET['code']) > 100){

        $url = 'https://graph.facebook.com/oauth/access_token';
        $tokenInfo = null;

        $token_url = "https://graph.facebook.com/oauth/access_token?"
                . "client_id=" . FACEBOOK_ID . "&redirect_uri=" .      urlencode(PATH_ROOT.REDIRECT_URI)
                . "&client_secret=" . FACEBOOK_SECRET . "&code=" . $_GET['code'];
            $curl = curl_init();
            curl_setopt($curl,CURLOPT_URL, $token_url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            parse_str(curl_exec($curl), $tokenInfo);
            curl_close($curl);
        echo "<pre>";
        print_r($tokenInfo);
        echo "</pre>";
///the three last rows get the message written above

1 个答案:

答案 0 :(得分:0)

错误的来源是,到目前为止,来自facebook的回复是一个字符串。 现在以json为榜样。 所以我改变了

parse_str(curl_exec($curl), $tokenInfo);

$tokenInfo= curl_exec($curl);
$tokenInfo = json_decode($tokenInfo, true); 

消息:此授权代码已被使用

此消息的出现可能是因为有几个申请一页的原因