1

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

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);
}
clzola
  • 1,925
  • 3
  • 30
  • 49

1 Answers1

1

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.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • I've tried with file_get_contents($createdFile->downloadUrl), but still gets 401 error. – clzola Mar 02 '15 at 16:50
  • 401 just means your access token is incorrect. You need to review how you are managing your token. Try logging the underlying http to see what's happening. – pinoyyid Mar 02 '15 at 23:54
  • Could you please share package you are using to download files from google drive – insoftservice Jan 15 '22 at 17:59