在开发基于Google服务的应用程序中,有很多的情况都需要获取Auth Token。
本文编写了一个简单的shell脚本文件,可方便程序开发人员快速的获取到Auth Token来调试程序。
本示例程序获取的Googel服务为ah,即Google App Engine,如果需要获取其他服务的token,只要修改脚本中的 service=ah 为自己的服务即可。
Google 常用服务和Service Name 对照表
| Google API | Service name |
|---|---|
| Google Analytics Data APIs | analytics |
| Google Apps APIs (Domain Information & Management) |
apps |
| Google Base Data API | gbase |
| Google Sites Data API | jotspot |
| Blogger Data API | blogger |
| Book Search Data API | print |
| Calendar Data API | cl |
| Google Code Search Data API | codesearch |
| Contacts Data API | cp |
| Documents List Data API | writely |
| Finance Data API | finance |
| Gmail Atom feed | mail |
| Health Data API | healthweaver (H9 sandbox) |
| Maps Data APIs | local |
| Picasa Web Albums Data API | lh2 |
| Sidewiki Data API | annotateweb |
| Spreadsheets Data API | wise |
| Webmaster Tools API | sitemaps |
| YouTube Data API | youtube |
Google Service 评测请参照 Google Account Service Names(Google工程师Tony Ruscoe)
脚本代码如下(get_auth_token.sh):
#!/bin/sh
# Get user email
read -p "Email: " EMAIL
# Get user password
stty -echo
read -p "Password: " PASSWORD; echo >&2
stty echo
# check and download cacert file
CACERTFILE="/tmp/cacert.pem"
if [ ! -f "$CACERTFILE" ]; then
wget http://curl.haxx.se/ca/cacert.pem -O $CACERTFILE
else
echo "cacert exist!!!"
fi
# Get a user auth token
echo Getting token from https://www.google.com/accounts/ClientLogin ... >&2
AUTH=$(CURL_CA_BUNDLE=$CACERTFILE
curl -S --location
https://www.google.com/accounts/ClientLogin
-d Email=$EMAIL
-d Passwd=$PASSWORD
-d accountType=HOSTED_OR_GOOGLE
-d source=carey
-d service=ah
--header 'Content-Type:application/x-www-form-urlencoded'
2>/dev/null
| awk -F= '$1 ~ /Auth/ {print $2}')
if [ -z "${AUTH}" ]; then
echo "Auth error" >&2
exit
else
echo "Auth="$AUTH
fi
程序使用如下:
$ ./get_auth_token.sh
Email: 输入自己的Google帐号
Password: 输入自己的Google密码
Auth=DQAAALIAAABNfSYKsytsIx_vzFOG_iHVO4Zv8su6hyn-5cTCtObnLTd1X8K0TDoNC5zdRZTg2NQtn6S47Jw1t3ZfE2J
qDhR_x4KezoWYufUloCVA_pkC3g1vAcy6KIGcqiSH2hecJa5UH9yuURyF7_ODS6ysjYZ0JC9WnuF3vsx_9mO4xFGOGtyw
xMYfGC-j801I1Pna0AJ48T-lCeU1yEiTyUUsLPVszWrvg5ae4BPOtTaZxD_lU4lJhYAXtvLNN1ZGdLJ_huI
最后可以利用获取到的Auth Token来访问google的服务了。




