首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >通过php脚本将文件匿名上传到google驱动器

通过php脚本将文件匿名上传到google驱动器
EN

Stack Overflow用户
提问于 2014-03-07 22:26:14
回答 2查看 2.9K关注 0票数 1

嗨,我正在用下面的php代码匿名上传文件到谷歌驱动器.

index.php文件

代码语言:javascript
代码运行次数:0
运行
复制
    <?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

代码语言:javascript
代码运行次数:0
运行
复制
<!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将文件上传到我的谷歌驱动器帐户。那似乎行不通..。我做错了什么,任何帮助都会很感激。谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-08 11:13:05

尽管有这些评论,但可以通过允许匿名用户通过应用程序上传到Google帐户。

只有一次,您需要身份验证作为自己,然后授权您的应用程序访问您的驱动器帐户使用脱机访问。这将创建一个刷新令牌。将该刷新令牌存储在服务器的某个位置。您可以通过编写一些只执行一次的代码来做到这一点,或者可以使用Oauth2游乐场。

然后,匿名用户可以将文件上传到应用程序,然后应用程序可以使用存储的刷新令牌将该文件上传到Google,以请求访问令牌。

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

票数 0
EN

Stack Overflow用户

发布于 2014-10-21 20:02:43

在@keaner的帖子链接中找到的,有可能

https://developers.google.com/drive/web/service-accounts

关于服务vs.regular帐户,来自文档

“由于无法访问服务帐户的Google Drive web用户界面,因此无法为这类帐户购买额外的Google驱动器存储空间。因此,您可能更喜欢使用常规帐户而不是服务帐户。”

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22261907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档