【问题标题】:anonymous upload of files to google drive via php script通过php脚本匿名上传文件到谷歌驱动器
【发布时间】: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


【解决方案1】:

尽管有 cmets,您可以允许匿名用户通过您的应用上传到您的 Google Drive 帐户。

只有一次,您需要以自己的身份进行身份验证,然后授权您的应用使用离线访问访问您的云端硬盘帐户。这将创建一个刷新令牌。将该刷新令牌存储在您的服务器中的某个位置。您可以通过编写一些只会执行一次的代码来做到这一点,或者您可以使用 Oauth2 Playground。

随后,匿名用户可以将文件上传到您的应用,然后您的应用可以使用存储的刷新令牌将该文件上传到您的 Google 云端硬盘以请求访问令牌。

另一种(可能更好的)方法是使用服务帐户。

【讨论】:

    【解决方案2】:

    在@keaner 的帖子链接中找到,有可能
    https://developers.google.com/drive/web/service-accounts

    关于服务与常规帐户,来自文档

    “由于无法访问服务帐户的 Google Drive 网络用户界面,因此无法为此类帐户购买额外的 Google Drive 存储空间。因此,您可能更喜欢使用普通帐户而不是服务帐号。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多