我想创建一个应用程序,在Google Cloud 端硬盘中的这些文件夹中添加文件夹和数据 .

我使用 refresh_token 值成功创建了一个acccess令牌 . 我试图做一个非常基本的文件上传,但它失败了 . 我也得到一个文件列表只是为了看到其他任何工作,当然是 .

出现文件列表后,我收到以下错误...

致命错误:未捕获的异常'Google_Service_Exception',并在/ var / www / html中显示消息'错误调用POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media:(403)权限不足' /google-api-php-client/src/Google/Http/REST.php:110堆栈跟踪:#0 /var/www/html/google-api-php-client/src/Google/Http/REST.php( 62):Google_Http_REST :: decodeHttpResponse(Object(Google_Http_Request),Object(Google_Client))1 [内部函数]:Google_Http_REST :: doExecute(Object(Google_Client),Object(Google_Http_Request))2 / var / www / html / google-api -php-client / src / Google / Task / Runner.php(174):call_user_func_array(Array,Array)3 /var/www/html/google-api-php-client/src/Google/Http/REST.php( 46):Google_Task_Runner-> run()4 /var/www/html/google-api-php-client/src/Google/Client.php(593):Google_Http_REST :: execute(Object(Google_Client),Object(Google_Http_Request) )5 /var/www/html/google-api-php-client/src/Google/Service/Resource.php(240):Google_Client-> execute(Object(Google_H in / var)第110行的/www/html/google-api-php-client/src/Google/Http/REST.php

UPDATE: 我尝试了这个,没有使用refresh_token,只是使用最新的访问令牌,但仍然得到完全相同的错误 .

Initially setting up access to get myself to the consent screen...

require_once ('../autoload.php');
ini_set('display_errors',1);
error_reporting(E_ALL);
session_start();

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setRedirectUri('http://www.example.com/oauth2callback.php');


if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    print_r($_SESSION);
    $client->setAccessToken($_SESSION['access_token']);
    $drive_service = new Google_Service_Drive($client);
    //$files_list = $drive_service->files->listFiles(array())->getItems();
    //echo json_encode($files_list);
} else {
    $redirect_uri = 'http:/www.example.com/google-api-php-client/oauth2callback.php';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

And then attempting to upload the file

error_reporting(E_ALL); ini_set('display_errors', 1);
require_once ('../autoload.php');

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');



$client->setAccessType('offline');
$client->setApprovalPrompt('force');

// this token is obtained the first time from refresh_token, it will not be shown after the initial requrest
$refreshToken ='xxx';

$client->refreshToken($refreshToken);
$service = new Google_Service_Drive($client);
$files_list = $service->files->listFiles(array())->getFiles();
echo '<pre>';
print_r($files_list);
echo '</pre>';
$client->addScope("https://www.googleapis.com/auth/drive");

DEFINE("TESTFILE", 'testfile-small.txt');
if (!file_exists(TESTFILE)) {
    $fh = fopen(TESTFILE, 'w');
    fseek($fh, 1024 * 1024);
    fwrite($fh, "!", 1);
    fclose($fh);
}

if ($client->getAccessToken()) {
    // This is uploading a file directly, with no metadata associated.
    $file = new Google_Service_Drive_DriveFile();
    $result = $service->files->create(
        $file,
        array(
            'data' => file_get_contents(TESTFILE),
            'mimeType' => 'application/octet-stream',
            'uploadType' => 'multipart'
        )
    );


}