i wanted to do the same thing for some PC app (language doesn't matter Java / Node / Command line Curl / or even client side JavaScript)
i couldn't found a simple explanation (beside @DaImTo here), but it works only for native apps,
in order to create some/any app i had to use "Web Application" OAuth Client Application type, these are the steps i used:
- goto: Google Cloud console
- create new project (if you haven't already)
- enable Google Drive API for the project from here
- click on "Google Drive API" and press the "Enable API" button
- create "OAuth consent screen" (the screen a user see to allow your app to access their google drive data)
- add your site address to the redirect url table
- create credentials "OAuth Client ID" for "Web Application" from here
- make sure to save the "client ID" and "client secret" we will use them next
Now that you have a project, consent screen and a WebClient as an App developer, we need to act as a client and approve the app to access our Google Drive data like this:
- Goto:
https://accounts.google.com/o/oauth2/auth?client_id=[YOUR CLIENT-ID]&redirect_uri=[YOUR SITE URL]&scope=https://www.googleapis.com/auth/drive&response_type=code&include_granted_scopes=true&access_type=offline&state=state_parameter_passthrough_value
- replace the values: [YOUR CLIENT-ID], [YOUR SITE URL]
- once you allow your app and a redirect to your site has occurred, copy from the url the: code=XXX parameter and save the XXX part aside as [YOUR CODE]
Once that's done, we can do again and again these next steps from command-line / code / whatever ...
- Get Access token by calling the next curl:
curl --request POST --data "code=[YOUR CODE]&client_id=[YOUR CLIENT ID]&client_secret=[YOUR CLIENT SECRET]&redirect_uri=[YOUR SITE URL]&access_type=offline&grant_type=authorization_code" https://oauth2.googleapis.com/token
response should look like this:
{
"access_token": "YYYY",
"expires_in": 3599,
"refresh_token": "ZZZZ",
"scope": "https://www.googleapis.com/auth/drive",
"token_type": "Bearer"
}
if you receive an error like this:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
it probably means the file type is incorrect, if you receive an error like this:
{
"error": "invalid_grant",
"error_description": "Bad Request"
}
it usually means that the user has revoked the app permission (or code obtained in step 9 has expired), so simply repeat step 9
to upload the file using curl, do this:
- upload a file using the "access-token" from the response (YYYY part):
curl -X POST -L -H "Authorization: Bearer YYYY" -F "metadata={name :'[FILE_NAME.EXT]'};type=application/json;charset=UTF-8" -F "file=@[FILE_NAME.EXT];type=application/javascript" "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
replace the [FILE_NAME.EXT] with your file name and file extension, and run the command from the folder where the file reside (or add the path to the file), for example:
curl -X POST -L -H "Authorization: Bearer YYYY" -F "metadata={name :'myScript.js'};type=application/json;charset=UTF-8" -F "file=@myScript.js;type=application/javascript" "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
this example uploads a javascript file, if you want to upload a different type - just replace the "application/javascript" with your file type, you can see accepted types here
in order to get the next access token, simply use the refresh token received in step 12 (the ZZZZ part) and use it in this curl command:
curl --request POST --data "access_type=offline&refresh_token=[ZZZZ]&client_id=[YOUR CLIENT ID]&client_secret=[YOUR CLIENT SECRET]&grant_type=refresh_token" https://oauth2.googleapis.com/token