【问题标题】:Upload file to google drive issue PHP将文件上传到谷歌驱动器问题 PHP
【发布时间】:2012-08-18 06:20:36
【问题描述】:

我在将文件上传到 Google 云端硬盘时遇到问题,我无法上传文件。任何人都可以帮忙,请回答。

以下是我上传文件时遇到的错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

我已使用驱动器 SDK 注册了应用程序,并且我正在使用驱动器 SDK 客户端 ID。此外,我每次都使用数据库中保存的长期刷新令牌中的更新访问令牌。

代码在这里:

$fname = basename($updfile); // where $updfile is the file name with path
$turl = "https://www.googleapis.com/drive/v1/files";
$jsondata = '
{
    "title": "'.$fname.'",
    "mimeType":"application/zip",
    "description":"The mailchimp data export to google drive."
}';

$headers = 'Host: www.googleapis.com'."\r\n";
$headers .= 'Authorization: Bearer '.$access_token."\r\n";  // $access_token is token generating everytime from refresh token
$headers .= 'Content-Type: application/json'."\r\n";
$headers .='Content-Length: '.strlen($jsondata)."\r\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $turl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsondata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($headers));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

如果有人对此有解决方案或建议,请回复/回答。 提前致谢。

【问题讨论】:

  • 你有想过这个吗?

标签: php file-upload http-status-code-401 google-drive-api


【解决方案1】:

我建议使用 PHP client library 并查看 PHP sn-p 的文档以了解如何将文件上传到 Google Drive:

https://developers.google.com/drive/v2/reference/files/insert

如果出于任何原因您想继续使用 php_curl 并手动构造 HTTP 请求,您应该尝试使用 OAuth 2.0 Playground 发送相同的请求并与您的比较。

【讨论】:

  • 除了您的“简单”上传示例之外,php 客户端库没有任何文档可用于执行此类操作。没有提到一个人如何使用“推荐的”可恢复上传功能。我希望 google 看看 jQuery.com 和 php.net 如何提供有关如何实现其库的文档。这就是他们“正常工作”的原因......谷歌只有一堆示例页面(大部分时间甚至不工作)和一个空的维基。更不用说您要求我们直接从与“发布”下载不同步的主干下载。非常不专业!!
【解决方案2】:

为什么不尝试在休息客户端中使用您的访问令牌来查看问题是来自您的 PHP 代码还是您的访问令牌。

Postman Rest Client for Chrome 使用起来非常简单。

【讨论】:

    【解决方案3】:

    我认为你的网址不正确:

    $turl = "https://www.googleapis.com/drive/v1/files";

    应该是:

    $turl = "https://www.googleapis.com/upload/drive/v1/files";

    这里是关于使用 v2 上传功能的文档(尽可能精简)。它可能会也可能不会比使用 v1 更有用:)

    https://developers.google.com/drive/manage-uploads

    【讨论】:

      猜你喜欢
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多