dqfwcj0030 2014-03-07 22:26
浏览 38
已采纳

匿名上传文件到谷歌驱动器通过PHP脚本

hi there i am using the following php code to upload files anonymously to google drive...

index.php file

    <?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';
?>

and the 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>

now the problem here is that every time i run the index.php on my web browser it asks me for authorization but what i am trying to do is let any random user with or without google drive accounts to upload files to my google drive account via the index.php. that does not seem to work.. what could i be doing wrong any help would be appreciated.. thanks

  • 写回答

2条回答 默认 最新

  • duanji9311 2014-03-08 11:13
    关注

    Despite the comments, you can allow anonymous users upload to your Google Drive account via your app.

    One time only, you need to authenticate as yourself and then authorise your app to access your drive account using offline access. This will create a refresh token. Store that refresh token somewhere in your server. You can do this by writing some code that will only execute once, or you can use the Oauth2 Playground.

    Then subsequently, an anonymous user can upload a file to your app, and then your app can upload that file to your Google Drive using the stored refresh token to request an access token.

    An alternative (and possibly better) approach is to use a Service Account.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配