如何使用服务帐户访问我的 Google 与 PHP 的联系人?

How do I access my Google Contacts with PHP using a service account?

我的 phone 服务器上有一个应用程序,它曾经使用客户端登录访问我的 Google 联系人,以允许我的 SIP phone 查找他们。现在客户端登录已被终止,我还没有找到一种方法来访问我的 Google 与 PHP 的联系人而不需要用户(我和我的妻子)进行任何身份验证。

// 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 用户,我该如何操作?似乎 Google 在他们杀死客户端登录时忘记了这一点 :(

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

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

我建议您尝试使用 Oauth2。