skyuuka
skyuuka

Reputation: 675

gdrive command line oauth2: cannot fetch token: 400 Bad Request

Suddenly, gdrive command line tool stops working, and always gives the following error message:

Failed to get about: Get https://www.googleapis.com/drive/v3/about?alt=json&fields=maxImportSizes%2CmaxUploadSize%2CstorageQuota%2Cuser: oauth2: cannot fetch token: 400 Bad Request
Response: {
  "error": "invalid_grant",
  "error_description": "Bad Request"
}

Upvotes: 16

Views: 20223

Answers (3)

Ivan Aracki
Ivan Aracki

Reputation: 5361

Managed to resolve this by retriggering gcloud authorization with:

gcloud auth application-default login

Upvotes: 13

林果皞
林果皞

Reputation: 7803

The steps I fixed it:

  1. rm ~/.gdrive/token_v2.json as other answer suggested.

  2. But I get danger error if I open the link get from gdrive about to re-authorize: enter image description here

  3. So I create a drive project by go to https://developers.google.com/drive/api/v3/quickstart/js and click "Enable the Drive API":

    enter image description here

  4. Click the "API Console" link on that dialog:

    enter image description here

  5. To make default redirect url (i.e. urn:ietf:wg:oauth:2.0:oob) works, we need Installed type app and can't use "Web application type app"(which only allow http/https scheme). So click "Create credentials" and choose "Oauth client ID" item:

    enter image description here

  6. Ensure select "Other" type:

    enter image description here

  7. Copy "Client ID" and "Client secret" from created item. cd to ~/.go/src/github.com/prasmussen/gdrive/, then replace both at this two variables in handlers_drive.go file, save it, e.g.:

        const ClientId = "xxx.apps.googleusercontent.com"
        const ClientSecret = "xxx"
    
  8. Then run go install at that directory. Now run gdrive about and open that link in web browser. I get This app isn't verified error page. You are the author who created that app on above steps and suppose safe. Nevertheless simply click "Advanced" link and click Go to Quickstart (unsafe) link: enter image description here

  9. Click "Allow" it, copy/paste the code back to gdrive about and done:

enter image description here

Upvotes: 2

ZektorH
ZektorH

Reputation: 2770

This is because your authorization token has expired.

Currently there is an open Issue to improve this error message here.

You should delete your token with rm ~/.gdrive/token_v*.json then run the gdrive about command to reauthorize

Upvotes: 6

Related Questions