如何在没有人工授权的情况下从php脚本访问我的谷歌驱动器

时间:2016-12-19 07:45:51

标签: php google-api google-drive-api google-oauth

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

2 个答案:

答案 0 :(得分: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
  7. 最后这是有效的代码片段:

    <?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;
    ?>
    

答案 1 :(得分:4)

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

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

PHP客户端库有一个使用服务帐户身份验证的示例,但它没有驱动器,您必须将书籍部分更改为Google驱动器。 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