谷歌登录后如何联系

时间:2017-01-08 16:10:10

标签: php google-api google-oauth google-api-php-client

我正在使用谷歌登录并使用此代码进行身份验证

$client = new Google_Client();
$client->setApplicationName("Google OAuth Login Example");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($simple_api_key);
$client->setAccessType('offline');
$client->setScopes(array('https://www.google.com/m8/feeds/',"https://www.googleapis.com/auth/userinfo.email"));
$objOAuthService = new Google_Service_Oauth2($client);

现在,这里从客户端获取访问令牌,并使用此访问令牌来获取用户联系人

$accessToken = client->getAccessToken();
  if ($client->getAccessToken()) {
    $userData = $objOAuthService->userinfo->get();
    $_SESSION['access_token'] = $client->getAccessToken();

 }

现在我想登录用户联系人用户这个api和访问令牌,我在谷歌用户的身份验证期间获得了

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=' . $max_results . '&oauth_token=' . $accessToken;
$xmlresponse = curl_file_get_contents($url);
echo 'contact result='.$xmlresponse;

但我获得了401 http状态,无法获取用户联系人。请帮助我如何使用访问令牌登录用户联系电子邮件,这是我在身份验证期间获得的。

1 个答案:

答案 0 :(得分:0)

正确的参数名称是access_token而不是oauth_token。

试试这个:

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=' . $max_results . '&access_token=' . $accessToken;
$xmlresponse = curl_file_get_contents($url);
echo 'contact result='.$xmlresponse;

注意:Google通讯录API是一种旧的GData API,您无法使用Google php客户端库来访问它。您应该查看Google People API,我发现返回的数据与Google联系人中的数据类似。