简体   繁体   中英

Facebook Graph API: Can't access the authorization token

What I want is to fetch data using of a public page. I tried the documentation, but wasn't able to find that information. Then I googled it and found this Can you get a public Facebook page's feed using Graph API without asking a user to allow? question here at stackoverflow and that perfectly explained what I want to do with PHP. I read in the McNab's answer that I need an app at facebook to fetch that data (to retrieve an access token). I created an app and it took me to the basic settings page where it gave me the AppId and the AppSecret . Now as from the McNab answer, I only need AppId and app secret to get the auth_token, I used the following code, but due to some reason I get false instead of the AuthToken. What am I doing wrong here? I didn't add any further details to the app, is it causing this problem, do I need to add further details to the app or is there anything else causing this?

PS Also I am trying this on localhost on my PC.

function fetchUrl($url){

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_TIMEOUT, 20);

 $feedData = curl_exec($ch);
 curl_close($ch); 

 return $feedData;

}

$profile_id = "100002138417834";

// App info needed for auth.
$app_id = "--------------";
$app_secret = "-=-=-=-=-=-=-=-=-=-=-=-";

$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={$app_id}&client_secret={$app_secret}");

var_dump($authToken);

UPDATE:

When I did var_dump(curl_error($ch)); (as suggested in the comments), it gave me this error:

string 'SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed' (length=146)

The ssl certificate is not correctly validated. Curl used to be able do this properly when it was bundled with CA certificates. Either you point curl to the right certificate ( info here http://curl.haxx.se/docs/sslcerts.html ), or disable validating all together.

You can disable ssl certificate validating using:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

This is a very common curl problem, the answer to this is probably all over SO ;-)

I tried to output the error (thanks to @Jack) and I got this:

string 'SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed' (length=146)

Seemed like a problem with SSL Certificate. So what I did was:

  1. Downloaded a ca bundle from here .
  2. Put that bundle in the same directory where I had this index.php file ie the file having the code.
  3. Then set the CURLOPT_CAINFO option using curl_setopt() to point to that file (it must be absolute path).
  4. Refreshed the page and it was working :)
  5. Here is the line to be added:

    curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__). '/cacert.pem');

And the credit goes to @Jack who helped me in the chat :)

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