使用Google Picker从Google云端硬盘下载文件 [英] Downloading file from Google Drive by using Google Picker

查看:215
本文介绍了使用Google Picker从Google云端硬盘下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个网页,用户可以在该网页上使用Google Picker从Google云端硬盘中选择图片,然后使用PHP脚本将所选文件下载到我的服务器上.

I'm trying to build a webpage on which user can select an image from Google Drive by using Google Picker and download selected files to my server by using PHP script.

我设法设置了Picker,我得到了文件ID,但是当我将此ID传递给后端并尝试GET方法时,我得到了一个身份验证错误.

I have managed to setup the Picker, and i get fileIDs but when i pass this IDs to my backend and try GET method i get an authentication error.

我花了2天的时间进行研究和研究,但是阅读Google官方文档越多,我就越困惑.

I have spent 2 days working on this and researching, but more I read google official documentation the more confused I am.

有人可以告诉我,还是链接我该示例的实施方式?是否可以通过某种方式将oAuthv2令牌从GooglePicker传递到我的PHP后端,然后将该令牌用于GET请求?

Can someone tell me, or link me the example how implement this? Is it possible to somehow pass the oAuthv2 token from the GooglePicker to my PHP backend and then use that token with the GET request?

非常感谢您!

这是我尝试获取 https://www.googleapis时遇到的错误.com/drive/v2/files/SOME_FILE_ID

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }

推荐答案

在调用GET之前,必须设置一个包含最新访问令牌的Authorization-Header.

Before you call the GET, you must set an Authorisation-Header containing an up to date access token.

如果要手动执行此操作,请执行以下步骤:-

If you want to do this manually, the steps are:-

  1. 使用您的app-id/client-id和范围请求权限.这将返回给您一个授权码.
  2. 使用授权码请求刷新令牌和访问令牌
  3. 存储刷新令牌以备将来使用
  4. 在http标头授权中设置访问令牌(类似于授权:Bearer ya29.AHES6ZR3HQa9trJM_IQcgNlM0SI4FvLQFiQfcAZCWLobfpjqtGlT6A)
  5. 发布您的GET
  1. Request permission using your app-id/client-id and scopes. This will return to you an authorization code.
  2. Use the authorization code to request a refresh token and an access token
  3. Store the refresh token for future use
  4. Set the access token in an http header Authorization (something like Authorization: Bearer ya29.AHES6ZR3HQa9trJM_IQcgNlM0SI4FvLQFiQfcAZCWLobfpjqtGlT6A)
  5. Issue your GET

通过单击 https://developers.google.com/oauthplayground,可以查看整个过程的实际运行情况/

或者,如果您的客户端应用程序已经具有访问令牌,则可以将其与文件ID一起发送到服务器,而您的服务器可以直接将其直接设置在Authorization标头中.

Alternatively, if your client app already has an access token, you could send that to your server alongside the file ID and your server can simply set that directly into the Authorization header.

有一些PHP库可供您使用,例如.转到 https://developers.google.com/drive/v2/reference/files/insert 并向下滚动以查看PHP示例.手动构建URL还是使用库完全由您选择.这些库的最大缺点是,如果出现问题,您确实需要无论如何都要理解和跟踪http才能看到正在发生的事情,因此从第一天开始就应该学习并喜欢它们.

There are PHP libraries you can use instead, eg. go to https://developers.google.com/drive/v2/reference/files/insert and scroll down to see the PHP samples. It's entirely your choice whether you build the URLs by hand or use the libraries. The big disadvantage to the libraries is that if something goes wrong, you really need to understand and trace the http anyway to see what's going on, so might as well get to learn and love them from day one.

消息超过未经身份验证的使用的每日限制"似乎使第一次计时器(包括我)感到困惑.这是未经身份验证的使用",这是重要的部分,这意味着您尚未设置授权"标头. Google API每天都有未经授权使用的配额(诸如URL Shorter之类的东西).对于云端硬盘,该配额为零,因此错误.

The message "Daily Limit for Unauthenticated Use Exceeded" seems to confuse first timers (me included). It's the "Unauthenticated Use" which is the important part, meaning you haven't set the Authorization header. Google APIs have a daily quota for unauthorized use (stuff like URL shortener). In the case of Drive, that quota is ZERO, hence the error.

这篇关于使用Google Picker从Google云端硬盘下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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