PHP Google Drive API-将文件上传到我自己的个人驱动器帐户

时间:2018-11-20 02:33:11

标签: php oauth google-drive-api

我正在尝试构建一段非常简化的代码,该代码只是将本地文件从服务器上传到我的个人驱动器帐户。现在,我正在为身份验证问题而苦苦挣扎,不确定我需要哪种凭据类型。

重要的是,由于这仅访问我自己的私有驱动器,因此我想一次上传我的凭据,以后不再询问。我根本不尝试使用oAuth访问用户的驱动器。似乎大多数文档都是针对我们是否要尝试验证和访问其他用户驱动器的?

我认为一旦进行身份验证就可以管理上传代码,但是现在我什至无法列出现有文件。

到目前为止,这是我的代码:

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

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient($credentialsPath = ''){
    $client = new Google_Client();
    $client->setApplicationName('Google Drive API');
    $client->setAuthConfig($credentialsPath); // saved some credentials from console, but not sure which ones to use? 
    $client->setScopes(Google_Service_Drive::DRIVE);

    return $client;
}


// Get the API client and construct the service object.
$client = getClient($credentialsPath);
$service = new Google_Service_Drive($client);

// Print the names and IDs for up to 10 files.
$optParams = array(
  'pageSize' => 10,
  'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);

if (count($results->getFiles()) == 0) {
    print "No files found.\n";
} else {
    print "Files:\n";
    foreach ($results->getFiles() as $file) {
        printf("%s (%s)\n", $file->getName(), $file->getId());
    }
}

运行此代码时,出现以下错误代码:

  

致命错误:未捕获的异常'Google_Service_Exception',消息为'{“ error”:{“ errors”:[{“ domain”:“ usageLimits”,“ reason”:“ dailyLimitExceededUnreg”,“ message”:“每日限制”,“ extendedHelp”:“ https://code.google.com/apis/console”,},“代码”:403,“消息”:“超出了未经身份验证的使用的每日限制。继续使用需要注册。” }

我看过的几件事表明此代码与每日限额无关,但实际上是凭据使用不正确。知道我该如何解决吗?

0 个答案:

没有答案
相关问题