Google Drive PHP API下载文件 [英] Google Drive PHP API Download file

查看:226
本文介绍了Google Drive PHP API下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用php api从谷歌驱动下载文件的问题。我设法上传文件到谷歌驱动器的文件夹和$服务 - >文件 - >插入返回给我的文件资源,其中属性downloadUrl但当我重定向到该网址,它不起作用。我得到401错误。这是为什么发生?我需要做什么使这项工作成为可能?

I have problem with downloading file from google drive using php api. I managed to upload file to google drive in folder and $service->files->insert returns to me File Resource which has attribute downloadUrl but when I redirect to that url, it does not work. I get 401 error. Why is this happening? What do I need to do make this work?

以下是代码:

Here's code:

<?php
require_once 'vendor/autoload.php';
session_start();

$client_id = 'xxx';
$client_secret = 'yyy';
$redirect_uri = 'http://localhost/GoogleDrive/code.php';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");

$service = new Google_Service_Drive($client);

$authUrl = $client->createAuthUrl();
header("Location: $authUrl");

和code.php

and code.php

require_once 'vendor/autoload.php';
session_start();

$client_id = 'xxx';
$client_secret = 'yyy';
$redirect_uri = 'http://localhost/GoogleDrivePayload/code.php';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setApprovalPrompt('force');
$client->addScope("https://www.googleapis.com/auth/drive");

$service = new Google_Service_Drive($client);


if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['upload_token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
    $client->setAccessToken($_SESSION['upload_token']);
    if ($client->isAccessTokenExpired()) {
        unset($_SESSION['upload_token']);
    }
}

if ($client->getAccessToken()) {
    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle("hello.exe");
    $file->setDescription("LALALALALAL");
    $file->setMimeType('application/x-msdos-program');

    $parent = new Google_Service_Drive_ParentReference();
    $parent->setId("zzz");
    $file->setParents(array($parent));

    $data = file_get_contents('hello.exe');
    $createdFile = $service->files->insert($file, array(
            'data' => $data,
            'mimeType' => 'application/x-msdos-program',
            'uploadType' => 'multipart',
            "visibility" => "DEFAULT"
    ));

    header("Location: " . $createdFile->downloadUrl);
}


推荐答案

downloadUrl被正是它说即,您的PHP应用程序可以用来获取文件的URL。您几乎不会将浏览器重定向到该URL,如果您确实(您可能真的不想这么做),则需要在URL上附加有效的访问令牌来修复401。

downloadUrl is exactly what it says, ie. the URL your PHP app can use to fetch the file. You would almost never redirect a browser to that URL, and if you did (you probably really don't want to), you'll need append a valid access token to the URL to fix the 401.

这篇关于Google Drive PHP API下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆