如何从PHP脚本访问我的谷歌驱动器,无需手动授权

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

我想写一个服务器php脚本,它将访问我的谷歌驱动器并在那里复制一个文件。服务器必须保存我的谷歌驱动器的凭据,而不是要求授权。我看到的所有示例都描述了各种用户可以在其驱动器上执行操作的Web应用程序。例如,https://developers.google.com/drive/v3/web/quickstart/php如何在我的服务器上保存所有需要的凭据。

php google-api google-drive-sdk google-oauth
2个回答
6
投票

经过长时间的研究和阅读谷歌文档和示例,我发现了一种适合我的方式。

  1. 您需要一个服务帐户。此帐户将访问Google云端硬盘数据。
  2. 我使用谷歌域名,因此我需要向此服务帐户授予域范围权限。
  3. Here you can find how to create a service account and grant it domain wide authority然而这个链接没有PHP示例
  4. Here you can find the same instructions with PHP examples
  5. 创建服务帐户时请注意您需要JSON类型的密钥文件。
  6. 我用过Google Application Default Credentials

最后这是工作代码片段:

<?php
require_once '/path/to/google-api-php-client/vendor/autoload.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$client->setSubject('email_of_account@you_want_to_work.for');

$service = new Google_Service_Drive($client);

//Create a new folder
$fileMetadata = new Google_Service_Drive_DriveFile(
     array('name' => 'Invoices', 
           'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array('fields' => 'id'));
echo $file->id;
?>

4
投票

我建议您考虑使用服务帐户。服务帐户就像是预先验证的虚拟用户。这意味着您可以使用服务帐户在Google云端硬盘帐户上共享文件夹,并且服务帐户可以上传到该帐户。我有一篇关于服务帐户如何工作的文章。 Google Developers console Service account

但是,在使用服务帐户时,您需要记住一些事项。默认情况下,服务帐户上传文件时的主要权限是拥有该文件,因此您需要在上载文件后授予您对该文件的个人帐户权限。这只是一个额外的步骤。

PHP客户端库有一个使用服务帐户身份验证的示例,但它没有驱动器,您将不得不将书籍部分更改为谷歌驱动器。 Service-account.php

require_once __DIR__ . '/vendor/autoload.php';
// Use the developers console and download your service account
// credentials in JSON format. Place the file in this directory or
// change the key file location if necessary.
putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service-account.json');
/**
 * Gets the Google client refreshing auth if needed.
 * Documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
 * Initializes a client object.
 * @return A google client object.
 */
function getGoogleClient() {
    return getServiceAccountClient();
}
/**
 * Builds the Google client object.
 * Documentation: https://developers.google.com/api-client-library/php/auth/service-accounts
 * Scopes will need to be changed depending upon the API's being accessed. 
 * array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS)
 * List of Google Scopes: https://developers.google.com/identity/protocols/googlescopes
 * @return A google client object.
 */
function getServiceAccountClient() {
    try {   
        // Create and configure a new client object.        
        $client = new Google_Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope([YOUR SCOPES HERE]);
        return $client;
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

这也可能有助于我的google drive samples

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