我需要帮助。 尝试从localhost将文件发送到Google驱动器。 1.我下载了示例项目并替换了我的凭据 然后我得到了:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart: (403) Access Not Configured'
<?php
session_start();
$url = 'http://localhost/api/';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId('1027833231791.apps.googleusercontent.com');
$client->setClientSecret('iriHG1HKCCYLphkSatrpOn_p');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code'])) {
$_SESSION['accessToken'] = $client->authenticate($_GET['code']);
header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
$client->authenticate();
}
$files= array();
$dir = dir('files');
while ($file = $dir->read()) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
}
$dir->close();
if (!empty($_POST)) {
$client->setAccessToken($_SESSION['accessToken']);
$service = new Google_DriveService($client);
$file = new Google_DriveFile();
foreach ($files as $file_name) {
$file_path = 'files/'.$file_name;
$mime_type = 'text/plain; charset=us-ascii';
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$service->files->insert(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type
)
);
}
header('location:'.$url);exit;
}
答案 0 :(得分:1)
确保您已启用“云端硬盘驱动程序API”(不是“云端硬盘驱动器SDK”;)除非您也想要“驱动器API”。我也犯了这个错误。