【问题标题】:How do I access my Google Contacts with PHP using a service account?如何使用服务帐户通过 PHP 访问我的 Google 通讯录?
【发布时间】:2015-08-14 18:22:55
【问题描述】:

我的电话服务器上有一个应用程序,该应用程序用于使用客户端登录访问我的 Google 通讯录,以允许我的 SIP 电话查找它们。现在客户端登录已被杀死,我还没有找到一种方法来使用 PHP 访问我的 Google 联系人,而无需用户(我和我的妻子)进行任何身份验证。

// create the instance of the Google Client
$this->googleclient = new Google_Client();
$this->googleclient->setApplicationName(GOOGLE_CLIENTID);

// define the credentials and access level (analytics readonly)
$credentials = new Google_Auth_AssertionCredentials(
    GOOGLE_EMAIL,
    array(
        'https://www.googleapis.com/auth/contacts.readonly',
    ),
    file_get_contents("config/key.p12"),
    "notasecret"
);

// set the user it wants to impersonate
$credentials->sub = GOOGLE_USER;

// throw the credentials into the client object
$this->googleclient->setAssertionCredentials($credentials);

// authenticate the application and fetch the token
$this->googleclient->setClientId(GOOGLE_CLIENTID);
$this->googleclient->getAuth()->refreshTokenWithAssertion();

$tokenData = json_decode($this->googleclient->getAccessToken());
$accessToken = $tokenData->access_token;

$val = file_get_contents("https://www.google.com/m8/feeds/contacts/".GOOGLE_USER."/full"
                        ."?v=3.0&access_token=".$accessToken);

print_r($val);

当我删除 GOOGLE_USER 常量并只允许服务帐户访问其自己的数据时,它可以工作,但这不是我的联系数据,也不是我妻子的数据。我知道 Google Apps 中有联系人委派,Apps 管理员也可以委派这些东西以允许服务帐户访问它。

但是,作为非 Google Apps 用户,我该怎么做呢?谷歌似乎在他们杀死客户端登录时忘记了这一点:(

【问题讨论】:

    标签: php google-api google-apps google-contacts-api


    【解决方案1】:

    服务帐户不是您,默认情况下无权访问任何数据。将服务帐户视为虚拟用户。如果您使用服务帐户电子邮件地址并将其作为用户添加到您的谷歌驱动器中的文件夹中,它将可以访问谷歌驱动器上的该文件夹。如果您使用服务帐户电子邮件地址并授予它访问您在 Google 日历上的日历之一的权限,它将可以访问该日历。

    据我所知,无法授予其他用户访问您的 Google 联系人的权限。

    我建议你尝试使用 Oauth2。

    【讨论】:

      猜你喜欢
      • 2017-08-22
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2020-10-25
      • 2012-10-10
      • 2017-11-17
      • 2013-12-27
      • 2019-07-25
      相关资源
      最近更新 更多