0

我无法使用以下代码交换令牌代码:

extract($_GET);
$url=urlencode('http://'.DOMAIN.'/admin/');
$app_id=FB_APPID;
$fb_token=FB_TOKEN;
$secret=FB_SECRET;

if(SEND_NEWS_TO_FB){
    if($code=='none'){
            $state=rand(100000000,9999999999999999999);
            $_SESSION['state']=$state;
            $script="
            <script type='text/javascript'>
                    var scope=encodeURI('publish_stream,user_status');
                    var app_id=$app_id;
                    var state=$state;
                    var url=$js_url;
                    window.location.href='https://www.facebook.com/dialog/oauth?                                                                                            client_id='+app_id+'&redirect_uri='+url+'&scope='+scope+'&state='+state;

            </script>";
            echo $script;
    }



if($code!='none' && $_SESSION['state']==$state && $_SESSION['fb_token']!=1){
            $_SESSION['fb_token']=1;
            $token_url="https://graph.facebook.com/oauth/access_token?"
            ."client_id=" .$app_id
            ."&redirect_uri=" .$url
            ."&client_secret=" .$secret
            ."&code=" .$code;
            $response = file_get_contents($token_url);
            $params = null;
            parse_str($response, $params);
            $token=$params['access_token'];
            $qry="UPDATE `settings` SET `FB_token`='$token'";
            $result=mysql_query($qry);
        }

将返回的 $token_url 放在我的浏览器中会给我一个带有令牌和过期时间的页面。运行脚本会返回一条非常模糊的“抱歉,出现问题”消息。我看到的几乎所有有问题的线程都是由于重定向 uri 的差异造成的,但是登录和令牌请求都从完全相同的位置获取 uri。

4

1 回答 1

0

There was a whitespace between the app_id value and '&redirect_url'. FYI, your browser (at least chromium) will urlencode the whitespace automatically which is why the url will work fine in the browser. I still am not sure how the space is there, but urlencode() fixes it for now.

于 2013-01-07T22:50:54.110 回答