Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌API PHP客户端-联系人服务_Php_Api_Google Api_Google Api Php Client_Google Contacts Api - Fatal编程技术网

谷歌API PHP客户端-联系人服务

谷歌API PHP客户端-联系人服务,php,api,google-api,google-api-php-client,google-contacts-api,Php,Api,Google Api,Google Api Php Client,Google Contacts Api,在经历了数小时的痛苦之后,我终于有点接近谷歌API PHP客户端的配置和使用,使用了(基于分析) 所以,现在我终于以一种合法和官方的方式证明了自己。我的自然想法是存在一个contrib/Google_ContactsService.php,但令我惊讶的是,它找不到其他32个服务类 我想从头开始。是否有任何方法可以合法和正式地获取特定用户的联系人?(有很多教程,但都是过时的和粗糙的) 编辑:我注意到有一个新版本的库可用,但仍然没有任何“联系人”服务中找到 编辑: 我的进步到目前为止。最后一行失败,

在经历了数小时的痛苦之后,我终于有点接近谷歌API PHP客户端的配置和使用,使用了(基于分析)

所以,现在我终于以一种合法和官方的方式证明了自己。我的自然想法是存在一个
contrib/Google_ContactsService.php
,但令我惊讶的是,它找不到其他32个服务类

我想从头开始。是否有任何方法可以合法和正式地获取特定用户的联系人?(有很多教程,但都是过时的和粗糙的)

编辑:我注意到有一个新版本的库可用,但仍然没有任何“联系人”服务中找到

编辑: 我的进步到目前为止。最后一行失败,来自Google的响应:
401。您的请求中有一个错误。
-我想这是因为缺乏权限(我没有请求联系人权限)。但是如果没有“Google_ContactsService.php”,我该怎么做呢?我迷路了。见代码:

<?php
session_start();

/**
 * Require the libaries
 */

    require_once 'assets/php/Google/Google_Client.php';
    require_once 'assets/php/Google/contrib/Google_AnalyticsService.php'; // from tutorial - I am supposed to get the Contacts Service, which is nowhere to find.

/**
 * Set up the Google_Client
 */

    $client = new Google_Client();
    $client->setAccessType('online'); // default: offline
    $client->setApplicationName($apiConfig['application_name']);
    $client->setClientId($apiConfig['oauth2_client_id']);
    $client->setClientSecret($apiConfig['oauth2_client_secret']);
    $client->setRedirectUri($apiConfig['oauth2_redirect_uri']);
    $client->setDeveloperKey($apiConfig['developer_key']); // API key

/**
 * $service implements the client interface, has to be set before auth call
 */

    $service = new Google_AnalyticsService($client);

/**
 * Log out
 */

    if (isset($_GET['logout'])) { // logout: destroy token
        unset($_SESSION['google_token']);
        exit('Logged out.');
    }

/**
 * Google auth code received
 *
 * Store access token
 */

    if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
        $client->authenticate();
        $_SESSION['google_token'] = $client->getAccessToken();
    }

/**
 * Set auth token
 */

    if (isset($_SESSION['token'])) { // extract token from session and configure client
        $token = $_SESSION['token'];
        $client->setAccessToken($token);
    }

/**
 * If no token, redirect and auth
 */

    if (!$client->getAccessToken()) { // auth call to google
        $authUrl = $client->createAuthUrl();
        header("Location: ".$authUrl);
        exit;
    }

/**
 * Get contacts
 */

    $access_token = json_decode($client->getAccessToken())->access_token;

    $url = 'https://www.google.com/m8/feeds/contacts/default/full?alt=json&v=3.0&oauth_token='.$access_token;

    $response =  file_get_contents($url);

    exit($response);

    echo 'Hello, world.';
来自:

不幸的是,contacts API是较早的GData API之一,而 此库适用于较新的API。您可以使用 库请求范围 (),并使用令牌 但您必须手动解析数据。泽德 框架确实有Zend_Gdata类,这些类可能会使阅读 结果容易一点


我发现。

我可以通过新的库让它工作。。。大部分。很明显,它不像一个完整的服务实现那么流畅,但它让事情进展顺利

Git是上面帖子中提到的库。 我从这里的例子开始:

oauth2callback.php(请注意,必须在开发人员控制台的API和身份验证/凭据部分列出此文件的完整路径,否则调用将失败):


然后是index.php:

<?php

set_include_path(get_include_path() . PATH_SEPARATOR . '/data/www/unity/html/lib');  # The path where I git got google-api-php-client

require_once 'google-api-php-client/src/Google/autoload.php';
$APPPATH = "/Applications/GFContacts"; # relative path from server root

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('accountinfo.json'); # JSON config file downloaded from the credentials page of my project https://console.developers.google.com/project
$client->addScope("https://www.googleapis.com/auth/contacts.readonly");

# Allow a param 'logout' to remove the access token - sometimes doing this helps debug issues
if (isset($_REQUEST['logout'])) {
    unset($_SESSION['access_token']);
    $client->revokeToken();

    print "You're logged out of Google";

    exit;
}

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

    $access_token = json_decode($_SESSION['access_token'])->access_token;

    $client->setAccessToken($_SESSION['access_token']);

    $req = new Google_Http_Request("https://www.google.com/m8/feeds/contacts/default/full");
    $val = $client->getAuth()->authenticatedRequest($req);

    // The contacts api only returns XML responses.
    $response = json_encode(simplexml_load_string($val->getResponseBody()));
    print "<pre>" . print_r(json_decode($response, true), true) . "</pre>"; 

} else {
    $redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . $APPPATH . '/oauth2callback.php';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

?>
getResponseBody());
打印“”。打印(json解码($response,true),true)。""; 
}否则{
$redirect_uri='https://'。$_SERVER['HTTP_HOST'].$APPPATH./oauth2callback.php';
标题('Location:'.filter_var($redirect_uri,filter_SANITIZE_URL));
}
?>

祝你好运!这篇文章更新已经有一段时间了,所以如果有人写过服务,我很想知道。同时,这应该让你开始

更新,2018年3月28日:

我已经创建了一个新的包来管理谷歌联系人,它基于更新的谷歌人API。如果你开始了一个新项目,我建议你使用这个软件包,而不是我在下面的原始帖子中提到的那个

您可以在此处找到更多详细信息:


原创帖子:

我最近不得不处理这个问题,在发现官方PHP Google客户端中缺少联系人服务后,我为Google Contacts API创建了一个(麻省理工学院许可的)PHP库

其中一个目的是真正简化所涉及的一些过程。因此,为了回答您的问题,在设置库之后,检索联系人只需以下代码

{
  "require": {
       "rapidwebltd/php-google-contacts-v3-api": "dev-master"
   }
}
该库需要做一些工作,但对于基本的联系人检索、创建和更新来说效果很好。如果需要,还可以通过
composer
安装。只需将以下内容添加到
composer.json中,然后运行
composer update

GitHub上提供了更多的安装说明和示例


GitHub链接:

好的,谢谢!它会被弃用吗?我现在使用最新版本,并已成功设置API。将来会有官方的API吗?或者?有人已经在新的库中实现了吗?我正在尝试提出“自定义”请求,但尚未成功:
require_once '../../../vendor/autoload.php';
use rapidweb\googlecontacts\factories\ContactFactory;
$contacts = ContactFactory::getAll();
if (count($contacts)) {
    echo 'Test retrieved '.count($contacts).' contacts.';
} else {
    echo 'No contacts retrieved!';
}
{
  "require": {
       "rapidwebltd/php-google-contacts-v3-api": "dev-master"
   }
}