如何上传文件public,DOWNLOADING未经授权可用?

时间:2013-04-17 08:05:27

标签: php google-drive-api

我上传了这样的文件:

    if (isset($_FILES[$name])) {
        $service = new Google_DriveService($client);

        //Insert a file
        $file = new Google_DriveFile();
        $file->setTitle($_FILES[$name]['name']);
        $file->setMimeType($_FILES[$name]['type']);
        $file->setParents(array('imgs'));

        $insertedFile = $service->files->insert($file, array(
            'data' => file_get_contents($_FILES[$name]['tmp_name']),
            'mimeType' => $_FILES[$name]['type']
        ));

        return $insertedFile;
    }

获取insertedFile

Array
(
    [kind] => drive#file
    [id] => 1sASTlgG7IFzcZyTUihp53uJbppFtxLs_GK_V2jYwapE
    [etag] => "vGmlhiWxP02tugPmRvLynwC_A0Y/MTM2NjE4NTYyNzQwOQ"
    [selfLink] => https://www.googleapis.com/drive/v2/files/1sASTlgG7IFzcZyTUihp53uJbppFtxLs_GK_V2jYwapE
    [alternateLink] => ...

但是当我去自我链接时,我收到错误

{
  ...
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

在没有任何授权的情况下获取上传文件的正确方法是什么?看起来文件只能通过API获得。

1 个答案:

答案 0 :(得分:0)

如果您要下载内容,请对下载网址发出经过身份验证的请求。

// Get the contents of the file.
$request = new Google_HttpRequest($file->downloadUrl);
$response = $client->getIo()->authenticatedRequest($request);
$content = $response->getResponseBody();