How can I create a HTTP/Rest request to upload som

2019-09-10 11:18发布

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?

1条回答
趁早两清
2楼-- · 2019-09-10 12:04

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:

  1. refresh_token - Used to get more access_tokens and never expires
  2. access_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.

  1. Obtain a OAuth 2.0 client ID through the Google Developers Console

  2. Obtain an access token from the Google Authorization Server

  3. Send the access token to an API

  4. Refresh the access token, if necessary

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):

There are several ways to make this request, and they vary based on the type of application you are building. For example, a JavaScript application might request an access token using a browser redirect to Google, while an application installed on a device that has no browser uses web service requests.

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

查看更多
登录 后发表回答