U
U
uranus2352014-07-17 21:30:45
PHP
uranus235, 2014-07-17 21:30:45

How to properly work with OAuth Facebook?

Trying to get basic Facebook user details using OAuth.
The code for getting the token is obtained.
To get the token I use file_get_contents

$params = array(
  "client_id"     => $app_id,
  "redirect_uri"  => $redirect_uri,
  "client_secret" => $app_secret,
  "code"          => $_GET["code"]
);

$query = http_build_query($params);

$url = "https://graph.facebook.com/oauth/access_token?{$query}"

When using file_get_contents it gives a "couldn't connect to server" error.
Returns nothing when using cURL
$url = "https://graph.facebook.com/oauth/access_token";
  
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($params)));
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($curl);
  curl_close($curl);
  $tokenInfo = json_decode($result);

  echo $tokenInfo;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
emissarman, 2014-07-17
@emissarman

Also try turning off CURLOPT_SSL_VERIFYHOST

G
Graph, 2014-07-18
@Graph

opauth.org

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question