【发布时间】:2014-03-07 22:26:14
【问题描述】:
您好,我正在使用以下 php 代码将文件匿名上传到谷歌驱动器...
index.php文件
<?php
session_start();
$url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $url_array[0];
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('Client ID');
$client->setClientSecret('Secret key');
$client->setRedirectUri($url);
$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";
$file->setTitle($file_name);
$file->setDescription('This is a '.$mime_type.' document');
$file->setMimeType($mime_type);
$createdFile = $service->files->insert(
$file,
array(
'data' => file_get_contents($file_path),
'mimeType' => $mime_type
)
);
// Uncomment the following line to print the File ID
//print 'File ID: %s' % $createdFile->getId();
}
header('location:'.$url);exit;
}
include 'index.phtml';
?>
还有index.phtml
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Google Drive Example App</title>
</head>
<body>
<ul>
<?php foreach ($files as $file) { ?>
<li><?php echo $file; ?></li>
<?php } ?>
</ul>
<form method="post" action="<?php echo $url; ?>">
<input type="submit" value="Send" name="submit">
</form>
</body>
</html>
现在这里的问题是,每次我在我的网络浏览器上运行index.php 时,它都会要求我授权,但我想要做的是让任何有或没有谷歌驱动器帐户的随机用户将文件上传到我的谷歌通过index.php 驱动帐户。这似乎不起作用..我可能做错了什么任何帮助将不胜感激..谢谢
【问题讨论】:
-
不会发生什么......我不能让其他用户将文件上传到我的谷歌驱动器......?
-
这个问题可能会提供一些见解[老问题][1] [1]:stackoverflow.com/questions/13269201/…
-
看过一千遍..
-
soo 基本上我不能做我想做的事......
-
是的,这就是我的意思
标签: php google-drive-api google-api-php-client