以编程方式获取Google云端硬盘APK access_token

时间:2016-11-01 14:30:20

标签: google-drive-api

我有一个基于Linux OS的自定义嵌入式系统。有相机连接到我的系统,我想使用谷歌驱动器作为云存储从我的相机拍摄的记录。

为此目的,我通过关注google drive api rest文档成功地使用了几乎所有google驱动器api功能

https://developers.google.com/drive/v3/web/about-sdk

Restful api函数需要access_token用于根据HTTP请求中的OAuth2协议进行身份验证。获得此令牌需要一次一次的手动操作。

1-使用我的帐户登录google时,我必须使用google开发者控制台创建client_id和client_secure。

2-然后我使用这些凭据来获取access_code。在对http请求的回复中,Google向我发送了一个网址和一个授权代码。

3-然后我使用浏览器访问网址,输入授权代码并允许手动进行身份验证。然后我用另一个HTTP请求获取access_token和refresh_token。

4-在那之后,我可以使用给定的access_token成功使用任何api函数。 (如果使用refresh_token过期,我会刷新它)

我的问题是为获取令牌而进行的一次手动操作。我在我的计算机上进行这些操作,因此在我的嵌入式系统中制作它们看起来非常困难。但我想在我的嵌入式系统中完成所有步骤(登录,client_id和client_secure,访问URL,键入授权代码并允许身份验证)。

所以,我想知道是否有可能在不使用开发者控制台和浏览器的情况下完成所有这一次手动过程来获取access_code?我能以编程方式制作它们吗?

这是我尝试关注oauth2 google官方文档。

创建client_id和client_Secret后,我只使用curl进行http操作。我为了测试目的编写了以下bash代码。因此,它不是自信,需要一些最初可以为空的文件并命名为" access_token"," folder_id"," refresh_token"," myFile& #34;同一目录中的文件。

#!/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

此致

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是使用服务帐户Domain-Wide Delegation of Authority。此方法适用于JWT进行身份验证。这个过程在这里解释:Using OAuth 2.0 for Server to Server Applications

应用程序将使用服务帐户进行身份验证,作为您要用于存储驱动器文件的ID。