1

我正在尝试提取我的 Google 联系人列表并在页面上显示姓名和电话号码。我发现了 Lorna Jane 的一篇有趣的帖子,并尝试了她的代码。我得到一个返回的令牌,但每次我重新访问该页面时,它都会要求我再次进行身份验证。使用当前代码,不提取数据数组:

$id = 'secret.apps.googleusercontent.com';
$scope = 'https://www.google.com/m8/feeds/default/full/';
$uri = 'http://example.com/callback.php';

$params = array(
  'response_type'    => 'code',
  'client_id'    => $id,
  'redirect_uri'    => $uri,
  'scope'        => $scope
);
$query = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($params);
header('Location: ' . filter_var($query, FILTER_SANITIZE_URL));
if (isset($_GET['code']))
{
  $code = $_GET['code'];
  $token = 'https://accounts.google.com/o/oauth2/token';
  $params = array(
      'code'        => $code,
      'client_id'    => $id,
      'client_secret'    => 'clientsecret',
      'redirect_uri'    => $uri,
      'grant_type'    => 'authorization_code'
  );
  $request = new HttpRequest($token, HttpRequest::METH_POST);
  $request->setPostFields($params);
  $request->send();
  $responseObj = json_decode($request->getResponseBody());
  var_dump($responseObj);
}

请让我知道我错过了什么。我更喜欢 pecl_http 实现,而不是 Google API 库。

4

0 回答 0