【问题标题】:PHP Google Drive API authentication not required不需要 PHP Google Drive API 身份验证
【发布时间】:2015-03-17 23:33:09
【问题描述】:

我正在开发一个使用 Google 驱动 API 的 PHP 代码。当您拥有该驱动器的 Google 帐户已登录时,该 API 可以上传文件。我的问题是,即使拥有它的谷歌账户没有登录,它是否有可能上传文件,例如,没有谷歌账户登录或其他谷歌账户登录。我只是想让驱动器公开,任何人都可以上传文件。

这是我使用的代码。

<?php

  function setGoogleDriveUpload($clientID, $secretKey, $redirecturi, $newfilename, $uploadfile){
    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($clientID);
    $client->setClientSecret($secretKey);
    $client->setRedirectUri($redirecturi);
    $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();
    }

    $client->setAccessToken($_SESSION['accessToken']);
    $service = new Google_DriveService($client);
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $file = new Google_DriveFile();

    $file_path = $uploadfile;
    $mime_type = finfo_file($finfo, $file_path);
    $file->setTitle($newfilename);
    $file->setDescription('This is a '.$mime_type.' document');
    $file->setMimeType($mime_type);
    $service->files->insert(
        $file,
        array(
            'data' => file_get_contents($file_path),
            'mimeType' => $mime_type
        )
    );

    finfo_close($finfo);

}

【问题讨论】:

  • “没有登录谷歌账户或登录其他谷歌账户”这句话甚至没有一点意义想要再试一次吗?请……

标签: php authentication google-api google-drive-api


【解决方案1】:

“我的问题是,即使拥有它的谷歌帐户没有登录,它是否可以上传文件”

可以,前提是帐户所有者授予了离线访问权限。在这种情况下,您的应用将收到一个刷新令牌,当您要上传文件时,您可以存储并使用它来生成访问令牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多