我有一个上传文件和下载他们与谷歌服务帐户的脚本.这看起来工作得很好,但现在下载时并非如此.当点击链接下载文件,而不是开始下载时,会出现Google Drive登录页面. 老实说,我不知道对Google Cloud配置的PHP代码进行了任何更改.我都判断过了,我看不出有什么变化或什么不对劲.我必须说我不是专家.

先谢谢你.

我已经判断了代码和Google配置.我期待着从php代码上传和下载文件到Google Drive.结果是上传文件很好,但当下载这些文件时,会出现Google Drive登录窗口,而不是下载文件.

require_once 'google-api-php-client/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=googlecredentials-path-file.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/drive.file']);
try {
   $service = new Google_Service_Drive($client);
  $google_file = new Google_Service_Drive_DriveFile();
  
  $file_path = 'buho.jpg';
    
  $google_file->setName($file_path);
  $google_file->setParents(['Google-Drive-folder-id']); 
  $google_file->setDescription('File uploaded from a PHP application');
  $google_file->setMimeType('image/jpeg');
  $result = $service->files->create(
    $google_file,
    array(
      'data' => file_get_contents($file_path),
      'mimeType' => 'image/jpeg',
      'uploadType' => 'media'
    )
  );
  echo '<a href="https://drive.google.com/open?id=' . $result->id . '" target="_blank">' . $result->name .'</a>';

推荐答案

您可以生成一个链接,使用该链接时,无需登录即可访问. 注意:任何人都可以通过该链接访问

<?php

require_once "google-api-php-client/vendor/autoload.php";
putenv("GOOGLE_APPLICATION_CREDENTIALS=googlecredentials-path-file.json");
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(["https://www.googleapis.com/auth/drive.file"]);
try {
    $service = new Google_Service_Drive($client);
    $google_file = new Google_Service_Drive_DriveFile();

    $file_path = "buho.jpg";

    $google_file->setName($file_path);
    $google_file->setParents(["Google-Drive-folder-id"]);
    $google_file->setDescription("File uploaded from a PHP application");
    $google_file->setMimeType("image/jpeg");
    $result = $service->files->create($google_file, [
        "data" => file_get_contents($file_path),
        "mimeType" => "image/jpeg",
        "uploadType" => "media",
    ]);
    // Set permissions to create a shareable link
    $permission = new Google_Service_Drive_Permission([
        "type" => "anyone",
        "role" => "reader",
    ]);
    $service->permissions->create($result->id, $permission);

    // Get the shareable link
    $file = $service->files->get($result->id, ["fields" => "webViewLink"]);
    $shareableLink = $file->webViewLink;

    echo '<a href="' .
        $shareableLink .
        '" target="_blank">' .
        $result->name .
        "</a>";
} catch (Exception $e) {
    // handle exception
}

Download file from url

<?php
    // add your shareable link here as the parameter.
    $fileContent = file_get_contents($shareableLink);
    $localFilePath = "./save-to-file-here.jpeg"
    if ($fileContent !== false) {
        // Save the file locally
        $result = file_put_contents($localFilePath, $fileContent);

        if ($result !== false) {
            echo "File downloaded successfully and saved as $localFilePath";
        } else {
            echo "Error saving the file locally.";
        }
    } else {
      echo "Error downloading the file content.";
   }

Php相关问答推荐

仅显示特定WooCommerce产品类别的额外产品字段

在AJX请求中使用简写后,FormData出现非法调用错误

在PHP中,如何将介于0和1之间(包括0和1)的浮点概率转换为数组索引?

如果为布尔值,则 for each 循环重新启动

如果WooCommerce购物车在 checkout 时被清空,请重定向至store 页面

如何在其他数组php中创建具有关联数组的数组

htaccess 配置提供静态文件和动态文件

PHP 8.2 弃用日志(log)存储在 Laravel 10.22.0 中的哪里?

服务器升级到新的mysql版本8.0.34后查询错误

如果 Select 的县不是store 所在的县,如何隐藏本地自提?

Google Drive API 服务->文件->get('root') 失败并显示找不到文件. (范围问题?)

在 WooCommerce 管理变量产品中启用其他变体图像

在 vue 自定义渲染器中访问变量

根据所选付款方式启用/禁用 WooCommerce 所需的 checkout 字段

Woocommerce的数量和价格条件

Laravel auth()->id() 在生产服务器中不工作

来自 GoDaddy 的邮箱在 Gmail 中显示为via

如何获取用户之间的最新消息列表

我怎样才能在 .htaccess 中有 2 个参数?

如何故意创建不同的错误(404,500,419)? Lavravel PHP