Skip to content

Instantly share code, notes, and snippets.

Created August 22, 2012 16:09
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save deanet/3427090 to your computer and use it in GitHub Desktop.
Save deanet/3427090 to your computer and use it in GitHub Desktop.
Uploading File into Google Drive (because grive too many dependencies qt, xorg ? )
## uploading to google
## rev: 22 Aug 2012 16:07
det=`date +%F`
browser="Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0.1"
accountype="HOSTED" #gooApps = HOSTED , gmail=GOOGLE
/usr/bin/curl -v --data-urlencode Email=$username --data-urlencode Passwd=$password -d accountType=$accountype -d service=writely -d source=cURL "" > $pewede/login.txt
token=`cat $pewede/login.txt | grep Auth | cut -d \= -f 2`
uploadlink=`/usr/bin/curl -Sv -k --request POST -H "Content-Length: 0" -H "Authorization: GoogleLogin auth=${token}" -H "GData-Version: 3.0" -H "Content-Type: $tipe" -H "Slug: $file" "" -D /dev/stdout | grep "Location:" | sed s/"Location: "//`
/usr/bin/curl -Sv -k --request POST --data-binary "@$file" -H "Authorization: GoogleLogin auth=${token}" -H "GData-Version: 3.0" -H "Content-Type: $tipe" -H "Slug: $file" "$uploadlink" > $pewede/goolog.upload.txt
Copy link

One more thing that might help get this working:

You may need to enable access to less secure apps at this page.

Also, there's a problem with the various enhanced scripts on this page: in order to determine if the type is HOSTED or GOOGLE, they check the username (email) to see if it's a Gmail address. This is not correct because you can now use a non-Gmail account to set up a Google account; that's what I'm doing. I have a non-Gmail account with email hosted somewhere else, but I'm using Google's other services (Drive).

Copy link

I think I am looking for this one. but I don't understand how to use these script. anyone explain?
Edit: OK, I got it now, great works.

Copy link

Great script - but having a problem getting it to run properly on a raspberry PI.

Weird problem. When I run it I'm getting "curl: (3) Illegal characters found in URL" in the final curl in the definition of $upload_link.
If I then edit the script and replace $upload_link variable with the actual $upload_link string it runs fine.

For example:
curl --request POST --output /dev/null --data-binary "@$file" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "$upload_link" $curl_args

replaced with:

curl --request POST --output /dev/null --data-binary "@$file" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "" $curl_args

works fine.

Any ideas?

Copy link

kane-c commented Feb 12, 2015

@nzbaxterman I ran into this problem too. The request to get the upload URL seems to have a line break in it now. Stripping that works for me:

upload_link = `curl ... | tr -d '\r\n'`

Copy link

sl45sms commented Feb 20, 2015

@kane-c tnx works for me

Copy link

semeion commented Apr 28, 2015

Nice work!

Anyone know how to avoid the file duplication?

Copy link

semeion commented Apr 28, 2015

Here have some info about how manage folders, could be nice get the list of folder names and overwrite the files to avoid duplication:

Here have info about how make an update case the file exist:

Here the author is using the new google drive api, where you can specify the 'uploadType':

I think we have elements to make a complete sync client in bash!

Copy link

does this still works? I always get

when authenticating. Seems like Google deprecated this kind of authentication

Copy link

deanet commented Oct 16, 2015

@nicolabeghin it doesn't work anymore.

Copy link

azizasm commented Dec 4, 2015

This code doesn't work anymore because google has enforced OAUTH 2.0.

Copy link

Grive doesn't depend on Qt or Xorg.

Copy link

XavM commented Jun 26, 2017

Google enforcing OAuth this script doesn't work any more;
You will get a 404 on "" when requesting the "${token}"

This one works fine with OAuth 2 :

You will need an API client and secret : Go to and create a "Google Drive" credential of type "OAuth client ID", sub type "other"

(Adding that here as this gist still popups first on google search)

Copy link

x011 commented Nov 11, 2019

evolution happens:

* automatically gleans MIME type from file

* uploads multiple files

* removes directory prefix from filename

* works with filenames with spaces

* uses dotfile for configuration and token

* interactively configuring

* uploads to target folder if last argument looks like a folder id

* quieter output

* uses longer command line flags for readability

* throttle by adding `curl_args="--limit-rate 500K"` to $HOME/.gdrive.conf
# based on
# useful $HOME/.gdrive.conf options:
#    curl_args="--limit-rate 500K --progress-bar"

browser="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"

destination_folder_id=${@: -1}
if expr "$destination_folder_id" : '^[A-Za-z0-9]\{28\}$' > /dev/null
    # all but last word
    set -- "${@:0:$#}"
    # upload to root
    unset destination_folder_id

if [ -e $HOME/.gdrive.conf ]
    . $HOME/.gdrive.conf

umask 0077

if [ -z "$username" ]
    read -p "username: " username
    unset token
    echo "username=$username" >> $HOME/.gdrive.conf

if [ -z "$account_type" ]
    if expr "$username" : '^[^@]*$' > /dev/null || expr "$username" : '.*$' > /dev/null

if [ -z "$password$token" ]
    read -s -p "password: " password
    unset token

if [ -z "$token" ]
    token=`curl --silent --data-urlencode Email=$username --data-urlencode Passwd="$password" --data accountType=$account_type --data service=writely --data source=cURL "" | sed -ne s/Auth=//p`
    sed -ie '/^token=/d' $HOME/.gdrive.conf
    echo "token=$token" >> $HOME/.gdrive.conf
umask $old_umask

for file in "$@"
    slug=`basename "$file"`
    mime_type=`file --brief --mime-type "$file"`
    upload_link=`curl --silent --show-error --insecure --request POST --header "Content-Length: 0" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "${destination_folder_id+/folder:$destination_folder_id/contents}?convert=false" --dump-header - | sed -ne s/"Location: "//p`
    echo "$file:"
    curl --request POST --output /dev/null --data-binary "@$file" --header "Authorization: GoogleLogin auth=${token}" --header "GData-Version: 3.0" --header "Content-Type: $mime_type" --header "Slug: $slug" "$upload_link" $curl_args

Still working, great !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment