简体   繁体   English

Facebook访问令牌

[英]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. 我正在尝试使用以下代码获取Facebook访问令牌:我之前确实创建了自己的函数,但该函数返回了相同的错误,并且我无法解决问题所在。

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. file_get_contents方法可能存在问题,因为您正在构建的url看起来不错。

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. 如果您搜索"facebook file_get_contents failed to open stream"您将在此问题上获得很多结果,而在堆栈溢出的地方很多。
It looks like a HTTPS problem with the file_get_contents as states in this thread: [function.file-get-contents]: failed to open stream . 看起来像HTTPS问题, file_get_contents作为此线程中的状态: [function.file-get-contents]:无法打开stream

Maybe try some other method of issuing http requests? 也许尝试发出HTTP请求的其他方法? 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. 只是为了进行测试,请尝试使用命令行中的curl与php脚本中尝试过的URL相同,然后看看会得到什么。

Looking at the request response, you haven't defined your Facebook application settings. 查看请求响应,您尚未定义Facebook应用程序设置。

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. 当您请求令牌时,您正在使用FB_APP_IDFB_SECRET_IDFB_PAGE_URL ,但是在响应中,我可以看到它们分别定义为:“ ID HERE”,“ SECRECT”和“ URLHERE”。

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. 我已经设法使用PHP SDK来解决此问题,并且在与SDK进行了长时间的争夺之后,使其能够执行我想要的操作。

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 查看此问题以获取此问题的答案: Facebook访问令牌服务器端身份验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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