问题描述
我想创建一个命令行工具来下载文件并将其上传到我的Google云端硬盘。我不想使用任何Google已经提供API调用的语言(Java,PHP,JavaScript,C#等)。
我将使用Haskell,但那是与问题无关。
我现在想做的是形成POST请求来上传文件。我的请求现在是这样的:
POST / upload / drive / v2 / files?uploadType = media HTTP / 1.1
主持人:www.googleapis.com
内容类型:* / *
内容长度:5
授权:持票人AIzaSy ...... jH683-FRO9GI
Cache-Control:no-cache
test1
我得到这个回应:
{
错误:{
错误:[
{
domain:global,
reason:authError,
message:无效凭证,
locationType:header,
location:Authorization
}
],
code:401,
message:无效凭证
}
}
在授权旁边:
我从Google Developer's Console创建的API密钥。
我想我必须做一个以上的请求认为做这项工作。首先,我必须获得授权码,然后使用该码获取授权令牌,然后将该令牌用于我的请求。
我该如何做到这一点?有人可以给我一个简短的例子吗?
#1
您不应该在您的请求中发送API密钥。
$ b
# 2
您的密钥可能已过期。它们只能持续60分钟。
#3
您的访问权限可能已被撤销。 (但在你的情况下,如果你能够获得访问令牌,则情况并非如此)
$ b
#4
您的请求中未包含访问令牌。
我认为您的问题是#1或#4,但我列出了其他可能的参考。
编辑#1: strong>
为解决评论,我将解释Google用来验证请求的过程。 。
最简单的步骤就是获取刷新标记,因为一旦将其存储在安全的位置,您就可以使用简单的httpRequest执行任何操作。
要获得刷新令牌,用户必须同意让项目访问某些功能。这在Google语言中称为范围
。您可以用许多不同的方法来做到这一点(根据谷歌的说法):
他们没告诉你的一点是,就你而言,使用什么媒介来获取 refresh_token
因为您只使用一个帐户。 Google Drive API的设计并非真正像服务器一样使用。它旨在供希望在其每个用户帐户中存储信息的开发人员使用。如果您有图片应用程序,则可以使用将某张图片存储在某人的Google云端硬盘帐户中的功能。
您(以及最近询问过的其他人)想要做的事情本质上是使用云端硬盘帐户作为服务器。这意味着您获取 refresh_token
的媒介不必与您使用云端硬盘帐户的媒介相关。
例如,在我的具体情况中,我想要一种将用户图片存储在服务器上以免安装Android应用程序的方式。我使用这个名为Parse的免费服务来充当我的数据库服务器,但是它们在你的免费服务中给你的文件存储非常有限。
因此,我决定尝试找出如何使用Google云端硬盘帐户来扩展我的存储空间。我创建了一个类似hostingaccount@gmail.com的通用gmail账户作为这些文件的主机(15g免费)。
为了得到我的 refresh_token
,我设置了一个php服务器来验证这个帐户。当有人访问我设置的页面时,系统会提示他们登录,然后授予我项目访问权限以使用他们的Google云端硬盘帐户(具体范围: https://www.googleapis.com/auth/drive
)。然后,我设置脚本在屏幕上为该帐户打印refresh_token。我将该字符串复制到服务器中,现在我可以轻松地发送httpRequests到:
https://www.googleapis.com/ oauth2 / v3 / token /
来获取访问令牌和:
https://www.googleapis。 com / upload / drive / v2 / files
来上传文件。
我将它链接到此编辑的顶部,但答案显示您如何使用我的php方法获取刷新令牌。我从来没有花时间去尝试弄清楚如何以其他方式获得刷新标记,但是如果您阅读了我的整个长篇答案,我想我会提到我相信这也可以通过Android Google Drive API完成。
我试图帮助这么多人解决这个问题,也许我应该开始一个博客并且为它做一个教程;)
I want to create a command line tool to download and upload files to my Google Drive. I do not want to use any language that Google already provides api calls (Java, php, javascript, C#, etc..).
I will use Haskell but that is not relevant with the question.
What I want to do now is form a POST request to upload a file. My request now is something like this:
POST /upload/drive/v2/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: */*
Content-Length: 5
Authorization: Bearer AIzaSy......jH683-FRO9GI
Cache-Control: no-cache
test1
and I get this response:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
Next to Authorization:
I put the API Key I created from Google Developer's console.
I figured I have to do more than one requests to make this work. First I have to get an auth code, then use that code to get an auth token and then use that token to my post request.
How can I make that happen? Can someone give me a short example?
There are several possible issues:
#1
You shouldn't be sending the API key with your requests.
#2
Your key might have expired. They only last for 60 minutes.
#3
Your access might have been revoked. (But in your case, if you are able to get an access token, then this is not the case)
#4
You did not include an access token in your request.
I think your problem is a result of #1 or #4, but I listed the other possibilities for reference.
Edit #1:
To address the comment, I will explain the process Google uses to authenticate requests. This question addresses some of the process but I am going to rewrite some of it in the context of this question. (The linked question was asked by me and then later answered by me)
There are two main items you need to make a request to a Google API:
refresh_token
- Used to get moreaccess_tokens
and never expiresaccess_token
- Used to send API calls (example: upload a file) and expires every 60 minutes.
In order to get either of these you have to follow certain steps.
More specific information about this "general" process can be found here.
The tricky step is getting the refresh token because once you have that stored in a secure location, you can do anything with simple httpRequests.
To get a refresh token, a user has to agree to give your project access to certain features. This is called scopes
in Google language. You can do this in many different ways (according to Google):
The one thing they don't tell you is that, in your case, it doesn't matter what medium you use to get the refresh_token
because you are only using one account. The Google Drive API isn't really designed to be used like a server. It is designed to be used by a developer that wants to store information on each of its user's accounts. Like if you had a picture app, you could have a feature that stores edited picture on someone's personal Google Drive account.
What you (and many others who have recently asked about) want to do is essentially use a Drive account as a server. That means that the medium through which you get your refresh_token
does not have to be related to the medium in which you are using the Drive Account.
For example in my specific case, I wanted a way to store user pictures on a server for free for an android app. I am using this free service called Parse to act as my database server, but they give you very limited file storage in their free tier.
Because of this, I decided to try and figure out how to use Google drive accounts to expand my storage. I created a generic gmail account something like "hostingaccount@gmail.com" to be the host of the files (15g for free).
To get my refresh_token
, I setup a php server to authenticate that one account. When someone goes to the page I setup, they are prompted to login and then grant access to my project to use their Google Drive account (Specific scope: https://www.googleapis.com/auth/drive
). I then setup the script to print the refresh_token for that account on the screen. I copied that string an put it into my server when now I can easily send httpRequests to:
https://www.googleapis.com/oauth2/v3/token/
to get an access token and to:
https://www.googleapis.com/upload/drive/v2/files
to upload files.
I link it at the top of this edit, but this answer shows you how to get a refresh token using my php method. I never spent the time to try an figure out how to get a refresh token any other way, but if you read my whole long answer I think I mention that I believe that this can also be done with the Android Google Drive API.
I have tried to help so many people with this problem, maybe I should just start a blog and make a tutorial about it ;)
这篇关于如何创建HTTP /休息请求以将某些内容上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!