通过Graph API检索用户个人资料时,Facebook PHP SDK访问 token 问题 - php

我试图在我的网站中检索用户个人资料。
但是获得访问令牌后,我将无法使用它。它显示的结果是

Graph returned an error: Invalid OAuth access token

我可以通过以下代码获取访问令牌

Login.php

session_start();

require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
  'app_id' => 'I have insert correct app id',
  'app_secret' => 'and app secret',
  'default_graph_version' => 'v2.0',
  ]);

$helper = $fb->getRedirectLoginHelper();
$permissions = ['email'];
$loginUrl = $helper->getLoginUrl('http://myweb.com/login-callback.php', $permissions);

echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
?>

下面是login-callback.php

<?php

session_start();

require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
  'app_id' => 'I have insert correct app id',
  'app_secret' => 'and app secret',
  'default_graph_version' => 'v2.0',
  ]);

  $helper = $fb->getRedirectLoginHelper();

  try {
    $accessToken = $helper->getAccessToken();
  } catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
  } catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
  }

  if (! isset($accessToken)) {
    if ($helper->getError()) {
      header('HTTP/1.0 401 Unauthorized');
      echo "Error: " . $helper->getError() . "\n";
      echo "Error Code: " . $helper->getErrorCode() . "\n";
      echo "Error Reason: " . $helper->getErrorReason() . "\n";
      echo "Error Description: " . $helper->getErrorDescription() . "\n";
    } else {
      header('HTTP/1.0 400 Bad Request');
      echo 'Bad request';
    }
    exit;
  }

  $_SESSION['fb_access_token'] = (string) $accessToken;

  try {
    $response = $fb->get('/me?fields=id,name', "'".$_SESSION['fb_access_token']."'");
  } catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
  } catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
  }

  $user = $response->getGraphUser();

  echo 'Name: ' . $user['name'];
  ?>

难道我做错了什么?请帮忙。谢谢。

参考方案

我只有:

$response = $fb->get('/me?fields=id,name', $token);
$user = $response->getGraphUser();

它完美地工作

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP-如何获取类的成员函数列表? - php

如果我知道班级的名字。有没有办法知道类的成员函数列表? 参考方案 get_class_methods()是你的朋友

php ziparchive类源代码 - php

Improve this question 我如何获取ziparchive类本身的源代码。 参考方案 假设您在谈论PHP ZipArchive class:下载PHP source code并查找适当的文件。如果您希望源代码是PHP代码,您可能会感到失望,因为源代码是用C语言编写的。或者,也可以在PHP Github Development Reposito…

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

PHP-如何建议搜索字词,“你是说……?” - php

当使用不检索任何结果的术语搜索数据库时,我想允许“您是不是……”建议(例如Google)。例如,如果有人寻找“ jquyer””,它将输出“ did you mean jquery?”当然,建议结果必须与数据库内部的值匹配(我正在使用mysql)。您知道可以做到这一点的图书馆吗?我已经用谷歌搜索过,但是没有找到任何好的结果。或者,也许您有一个想法,该如何独自…