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语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?