【发布时间】:2016-11-01 14:30:19
【问题描述】:
我有一个基于 linux 操作系统的定制嵌入式系统。有相机连接到我的系统,我想使用谷歌驱动器作为从我的相机拍摄的记录的云存储。
为此,我按照中的 google drive api rest 文档成功使用了几乎所有 google drive api 功能
https://developers.google.com/drive/v3/web/about-sdk
Restful api 函数需要一个 access_token 来根据 HTTP 请求中的 OAuth2 协议进行身份验证。获取此令牌需要一次性手动操作。
1- 使用我的帐户登录谷歌时,我必须使用谷歌开发者控制台创建一个 client_id 和一个 client_secure。
2- 然后我使用这些凭据获取 access_code。作为对 http 请求的响应,谷歌向我发送了一个 url 和一个授权代码。
3- 然后我使用浏览器访问 URL,输入授权代码并允许手动进行身份验证。然后我通过另一个 HTTP 请求获取 access_token 和 refresh_token。
4- 在那之后,我可以通过给定的 access_token 成功使用任何 api 函数。 (如果它过期,我会使用 refresh_token 刷新它)
我的问题是一次性手动操作以获得令牌。我正在我的计算机中进行这些操作,因此在我的嵌入式系统中进行这些操作看起来非常困难。但我想在我的嵌入式系统中完成所有步骤(登录、client_id 和 client_secure、访问 url、输入授权代码和允许身份验证)。
所以,我想知道是否有可能在不使用开发人员控制台和浏览器的情况下一次性完成所有这些手动过程来获取 access_code?我可以以编程方式制作它们吗?
这是我在 oauth2 谷歌官方文档之后尝试的。
创建 client_id 和 client_Secret 后,我只是使用 curl 进行 http 操作。我为测试目的编写了以下 bash 代码。所以它不自信,需要一些最初可以为空的文件,并在同一目录中命名为“access_token”、“folder_id”、“refresh_token”、“myFile”文件。
#!/bin/bash
# Google Drive API
get_file_id() {
RESPONSE=`curl --silent -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/drive/v2/files?q=title+contains+\'$1\'\&fields=items%2Fid`
FILE_ID=`echo $RESPONSE | python -mjson.tool | grep -oP 'id"\s*:\s*"\K(.*)"' | sed 's/"//'`
}
set -e
CLIENT_SECRET="my client_secret"
CLIENT_ID="my_client_id"
BOUNDARY="foo_bar_baz"
SCOPE="https://docs.google.com/feeds"
MIME_TYPE="application/octet-stream"
ACCESS_TOKEN=`cat access_token`
REFRESH_TOKEN=`cat refresh_token`
FOLDER_ID=`cat folder_id`
if [ "$1" == "create_token" ]; then # Usage: <"create_token">
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/device/code" -d "client_id=$CLIENT_ID&scope=$SCOPE"`
DEVICE_CODE=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'device_code"\s*:\s*"\K(.*)"' | sed 's/"//'`
USER_CODE=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'user_code"\s*:\s*"\K(.*)"' | sed 's/"//'`
URL=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'verification_url"\s*:\s*"\K(.*)"' | sed 's/"//'`
echo -n "Go to $URL and enter $USER_CODE to grant access to this application. Hit enter when done..."
read
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" -d "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&code=$DEVICE_CODE&grant_type=http://oauth.net/grant_type/device/1.0"`
ACCESS_TOKEN=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'access_token"\s*:\s*"\K(.*)"' | sed 's/"//'`
REFRESH_TOKEN=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'refresh_token"\s*:\s*"\K(.*)"' | sed 's/"//'`
echo "Access Token: $ACCESS_TOKEN"
echo "Refresh Token: $REFRESH_TOKEN"
echo "$ACCESS_TOKEN" > access_token
echo "$REFRESH_TOKEN" > refresh_token
elif [ "$1" == "refresh_token" ]; then # Usage: <"refresh_token">
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&refresh_token=$REFRESH_TOKEN&grant_type=refresh_token"`
ACCESS_TOKEN=`echo $RESPONSE | python -mjson.tool | grep -oP 'access_token"\s*:\s*"\K(.*)"' | sed 's/"//'`
echo "Access Token: $ACCESS_TOKEN"
echo "$ACCESS_TOKEN" > access_token
elif [ "$1" == "create_folder" ]; then # Usage: <"create_folder">
FOLDER_NAME=`date "+%F-%T"`
( echo -en "{ \"title\": \"$FOLDER_NAME\", \"mimeType\": \"application/vnd.google-apps.folder\" }\n" ) \
| curl -H 'GData-Version: 3.0' -v "https://www.googleapis.com/drive/v2/files" \
--header "Authorization: Bearer $ACCESS_TOKEN" \
--header "Content-Type: application/json" \
--data-binary "@-"
#save FILE_ID to filde
get_file_id $FOLDER_NAME
echo "$FILE_ID" > folder_id
elif [ "$1" == "upload_file" ]; then # Usage: <"upload_file"> <file name>
( echo -en "--$BOUNDARY\nContent-Type: application/json; charset=UTF-8\n\n{ \"title\": \"$2\", \"parents\": [ { \"id\": \"$FOLDER_ID\" } ] }\n\n--$BOUNDARY\nContent-Type: $MIME_TYPE\n\n" \
&& cat $2 && echo -en "\n\n--$BOUNDARY--\n" ) \
| curl -H 'GData-Version: 3.0' -v "https://www.googleapis.com/upload/drive/v2/files/?uploadType=multipart" \
--header "Authorization: Bearer $ACCESS_TOKEN" \
--header "Content-Type: multipart/related; boundary=\"$BOUNDARY\"" \
--data-binary "@-"
elif [ "$1" == "list_files" ]; then # Usage: <"list_files"> <number of files>
curl -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/drive/v2/files?maxResults=$2
elif [ "$1" == "download_file" ]; then # Usage: <"download_file"> <file name>
get_file_id $2
curl -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/drive/v2/files/$FILE_ID?alt=media
elif [ "$1" == "get_file" ]; then # Usage: <"get_file"> <file name>
get_file_id $2
curl -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/drive/v2/files/$FILE_ID
elif [ "$1" == "delete_file" ]; then # Usage: <"delete_file"> <file name>
get_file_id $2
curl -X Delete -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/drive/v2/files/$FILE_ID
elif [ "$1" == "trash_file" ]; then # Usage: <"trash_file"> <file name>
get_file_id $2
curl -d -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/drive/v2/files/$FILE_ID/trash
elif [ "$1" == "untrash_file" ]; then # Usage: <"untrash_file"> <file name>
get_file_id $2
curl -d -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \
https://www.googleapis.com/drive/v2/files/$FILE_ID/untrash
fi
exit 0
问候
【问题讨论】:
-
如果对您有帮助,请查看SO question。
-
它在 oauth2 谷歌官方文档中。告诉我们你从那里尝试了什么。
-
创建client_id和client_Secret后,我使用curl进行http操作。
标签: google-drive-api