我需要一些Google API Oauth解决方案

时间:2013-07-19 09:56:18

标签: php google-api

我正在尝试提取我的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库。

0 个答案:

没有答案
相关问题