Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌驱动PHP API下载文件_Php_Google Drive Api - Fatal编程技术网

谷歌驱动PHP API下载文件

谷歌驱动PHP API下载文件,php,google-drive-api,Php,Google Drive Api,我在使用php api从google drive下载文件时遇到问题。我设法上传文件到谷歌驱动器文件夹和$service->files->insert返回给我的文件资源,该资源具有属性downloadUrl,但当我重定向到该url时,它不起作用。我得到401错误。为什么会这样?我需要做些什么才能让它工作 以下是代码: <?php require_once 'vendor/autoload.php'; session_start(); $client_id = 'xxx'; $client_

我在使用php api从google drive下载文件时遇到问题。我设法上传文件到谷歌驱动器文件夹和$service->files->insert返回给我的文件资源,该资源具有属性downloadUrl,但当我重定向到该url时,它不起作用。我得到401错误。为什么会这样?我需要做些什么才能让它工作

以下是代码:

<?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");

downloadUrl就是它所说的,即PHP应用程序可以用来获取文件的URL。您几乎永远不会将浏览器重定向到该URL,如果您这样做了(您可能真的不想这样做),您将需要向URL附加一个有效的访问令牌来修复401。

我尝试过使用文件获取内容($createdFile->downloadUrl),但仍然得到401错误。401只是意味着您的访问令牌不正确。您需要查看您是如何管理令牌的。尝试记录底层http以查看发生了什么。
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);
}