如何通过google / apiclient从PHP使用Google Drive API的API密钥

问题描述 投票:2回答:2

我希望我的PHP Web应用程序在特定的Google帐户(不是任意用户的帐户,而是我们公司的特定帐户)中创建电子表格。

我已经测试并可以正常工作的东西

我需要google/apiclient PHP客户端库。它已启动并正在运行。

我在控制器中有那些私有方法:

private function getGoogleDriveService( string $scopes ) : \Google_Service_Drive
{
    $client = $this->getClient( $scopes );

    $service = new \Google_Service_Drive( $client );

    return $service;
}

private function getClient( string $scopes ) : \Google_Client
{
    $client = new \Google_Client();

    $client->setApplicationName( 'My nice application name.' );
    $client->setAuthConfig( $this->getSecretPath() );
    $client->setScopes( $scopes );
    $client->setAccessType( 'offline' );

    return $client;
}

private function getSecretPath() : string
{
    $projectDir = $this->get( 'kernel' )->getProjectDir();
    $credentialsFullPath = $projectDir . '/app/config/googleApiSecret.json';

    return $credentialsFullPath;
}

我称呼getGoogleDriveService告诉我想要特定控制器操作的范围,以创建Google服务。首先通过调用getClient创建服务,该服务返回已初始化的\Google_Client对象,该对象又将传递给其中的credentials.json路径。

到目前为止,很好。这有效。

例如,使用这样的代码:

$driveService = $this->getGoogleDriveService( \Google_Service_Drive::DRIVE );

$file = new \Google_Service_Drive_DriveFile();
$file->setName( 'My nice dummy file' );
$file->setMimeType( 'application/vnd.google-apps.spreadsheet' );
$file = $driveService->files->create( $file );

$feedbackMessage = sprintf( 'Created spreadsheet via DRIVE API with Id: %s', $file->id );

但有障碍!!这正在作为“服务帐户”。它在“机密”位置创建和修改文件,而这些位置在Drive Web前端不可见。

会发生什么

我有一个用户(例如称它为[email protected]),并且该用户是创建“服务帐户”的用户。凭证文件包含某种“虚拟电子邮件地址”,或多或少与此类似:[email protected]

。json或多或少像这样一个:

{
  "type": "service_account",
  "project_id": "my-nice-super-project",
  "private_key_id": "7777777777777788888888888888899999999999",
  "private_key": "-----BEGIN PRIVATE KEY-----\nxxx[...]xxx==\n-----END PRIVATE KEY-----\n",
  "client_email": "[email protected]",
  "client_id": "123456789123456789123",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/alice%40my-nice-super-project.iam.gserviceaccount.com"
}

会发生什么:

  1. 如果我使用[email protected]登录到Google驱动器(使用浏览器),并且手动创建了电子表格文件Hello-1,则无法从应用程序中读取它。
  2. 如果从登录为Hello-1的网站上手动共享文件[email protected]并将其共享到[email protected],则可以从应用程序中读取文件。但是我无法将其从应用程序中删除。
  3. 如果我从应用程序创建文件Hello-2,则从[email protected] Web前端看不到它。

[似乎即使服务帐户是从[email protected]创建的,它也可以用作完全独立的存储,并且来自“人类Alice”和“机器人Alice”的文件被视为来自不同用户。

我需要

我想要的是应用程序可以在不提示OAuth权限的情况下读取/写入[email protected]的文件。

因此,要明确一点:我不希望我的PHP Web应用程序能够“在得到他的同意的情况下编辑任何人的云端硬盘,但是我想让任何人都可以编辑Alice's Drive”。

原因是我们已经拥有Alice帐户,就像某些文档的“中央存储”。

公司中的多个代理商必须能够通过应用程序编辑内容,只有老板才能通过Google云端硬盘网站登录。代理商将没有爱丽丝密码,因此他们无法通过OAuth同意。但是该公司拥有Alice帐户,因此我们可以在此处输入并创建API密钥,然后在服务器中进行设置。

我被困的地方

我可以设法通过服务帐户使该软件正常工作。但这不是我想要的:我希望该软件在“ Alice的文档”上运行,而不是在“ Alice服务帐户的文档”上运行,因为它们似乎位于不同的环境中。

我不知道如何初始化

$service = new \Google_Service_Drive( $client );

因此它无需OAuth同意即可与Alice的文件一起使用。

google-api google-drive-api google-api-php-client api-key service-accounts
2个回答
1
投票
  • 您想使用服务帐户在[email protected]的Google云端硬盘中写入和读取文件。
  • 您想要使用服务帐户将[email protected]的文件写入和读取。
  • 您想用浏览器查看文件。

为了实现上述情况,我想提出以下方法。请认为这只是几个可能的答案之一。

方法:

  1. [email protected]的驱动器中创建一个文件夹,并与服务帐户共享该文件夹。
  2. 带有服务帐户的应用程序在共享文件夹中创建新文件时,将文件的所有者更改为[email protected]并与服务帐户共享。

通过以上方法,应用程序可以读写[email protected]的文件,并且[email protected]可以使用浏览器以自己的帐户查看该文件。

注意:

  • [email protected]的驱动器不同于服务帐户的驱动器。并且浏览器无法直接看到服务帐户的云端硬盘。因此,在这种方法中,使用了份额。

参考:


0
投票

使用服务帐户且如果有[G Suite Account,则可以使用[Domain-Wide Delegation of Authority您的服务帐户[email protected]将可以模拟您想要的任何用户,在这种情况下为您的[email protected]

这将帮助您使用已有的代码在[email protected]驱动器中创建文件。您只需要修改代码的这一部分:

private function getClient( string $scopes ) : \Google_Client
{
    // Add this line of code
    // User you want to "impersonate"
    $user = "[email protected]"

    $client = new \Google_Client();

    $client->setApplicationName( 'My nice application name.' );
    $client->setAuthConfig( $this->getSecretPath() );
    $client->setScopes( $scopes );
    $client->setAccessType( 'offline' );
   // Add this line of code
   $client->setSubject( $user );

    return $client;
}

HERE,您可以查看有关PHP的Google API客户端库的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.