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

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 - 通过Graph API检索用户个人资料时,Facebook PHP SDK访问 token 问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32738697/

10-09 15:43
查看更多