2013/07/02

利用 curl 上傳檔案到 google drive

看到一篇 google.sh 的文章討論「利用 curl  上傳檔案到  google drive」這件事,裡面的回應改良了 script 的寫法,把帳號密碼跟 script 分開是比較優良的寫法,因此在此跟大家分享。

該回應中的 script 有點 bug, 修正如下:

===============================================
if [ ! -f $1 ];then
        echo "please provide a file in arg"
        exit -1
fi
creditFile=~/bin/credits.ini
if [ ! -f $creditFile ];then
        echo "please fill $creditFile (file has just been created)"
        echo "username=$login
password=$pass">$creditFile
        exit -1
fi

file="$1"
browser="Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0.1"
. $creditFile

accountype="GOOGLE" #gooApps = HOSTED , gmail=GOOGLE
mime_type=`file -b --mime-type $file`

/usr/bin/curl -v --data-urlencode Email=$username --data-urlencode Passwd=$password -d accountType=$accountype -d service=writely -d source=cURL "https://www.google.com/accounts/ClientLogin" > /tmp/login.txt

token=`cat /tmp/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: $mime_type" -H "Slug: $file" "https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false" -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: $mime_type" -H "Slug: $file" "$uploadlink" > /tmp/google-log.upload.txt

============
第一次執行時,會產生 credits.ini 然後就離開了, 請自行填滿它,存檔後再次執行即可,用法如下:

google-drive.sh UPLOAD.FILE

譬如:
google-drive.sh google-drive.sh  # 把自己上傳

0 意見: