I'm trying to get the list of files on Google Drive with curl, but OAuth 2 is getting the best of me.
Here are some of the things I tried:
curl -H "Authorization: Bearer $token" https://www.googleapis.com/drive/v2/files
Where $token is a 460 character string I got using:
https://www.google.com/accounts/ClientLogin
and this upload script (which works great). This is the error I received:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
Also tried:
curl https://www.googleapis.com/drive/v2/files?key=apiKey
Error:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
And:
curl -H "Authorization: GoogleLogin auth=${token}" "https://www.googleapis.com/drive/v2/files"
Error:
{
"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."
}
}
I had little success with both the JavaScript and PHP client libraries, both seem optimized for the situation where the user provides log/pass in order to authorize the app. What I need is a way to list the files from a single account, every time.
I just spent like 30 minutes to figure this out myself for accessing contacts API and redid the steps for drive API and documented them for future reference. There are 5 steps and the first 4 are one time setup.
Step 1: Create new OAuth2 credentials
Note down the
client ID
andclient secret
.Step 2: Request authorization with drive as scope.
Using the
client ID
andhttps://docs.google.com/feeds
asscope
, construct the below curl command:Copy the
user_code
.Step 3: Authorize the request.
Visit the verification URL https://www.google.com/device and enter the copied code.
Step 4: Obtain
access_token
.Add the
device_code
obtained from the auth request to theclient ID
andclient secret
and construct the below curl command:We now have the required
access_token
, save it and use it with all the drive REST API requests.Step 5: Drive API request.
For future googlers:
If you want to save yourself an afternoon of pain, forget google's doumentation and head over here
The gist of it, since I know stackoverflow prefers quoting the content to linking:
In your browser:
Allow the access, of course, and copy the code which should look like 4/v6xr77ewYqjkslsdUOKwAzu
You’ll get a JSON like this one:
If you curl:
you’ll get something like:
Done
Renew the token
You have to use the “refresh_token” received previously
and you’ll get a new access_token.