当前位置: 首页 > 知识库问答 >
问题:

以编程方式获取Google Drive APK access_token

骆昊阳
2023-03-14

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

3-然后我使用浏览器访问url,键入授权代码并允许手动身份验证。然后通过另一个HTTP请求获得access_token和refresh_token。

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

我的问题是一次手动操作是为了获得令牌。我是在我的电脑中做这些操作,所以在我的嵌入式系统中做它们看起来很困难。但我想在我的嵌入式系统中完成所有步骤(登录、client_id和client_secure、访问url、键入授权代码和允许身份验证)。

#!/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个答案

师建德
2023-03-14

这样做的一种方法是使用域范围的授权,即服务帐户。此方法与用于身份验证的JWT一起工作。这里解释了这个过程:将OAuth2.0用于服务器到服务器的应用程序

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

 类似资料:
  • 问题内容: 如何从包含证书和私钥的PEM文件中以编程方式获取KeyStore?我试图在HTTPS连接中向服务器提供客户端证书。我已经确认,如果我使用openssl和keytool来获取jks文件(该文件是动态加载的),则客户端证书可以使用。我什至可以通过动态读取p12(PKCS12)文件来使其工作。 我正在考虑使用BouncyCastle的PEMReader类,但无法克服一些错误。我正在使用-Dj

  • 问题内容: 我正在编写一个报告本地计算机上网络设备属性的应用程序。我需要mac地址,mtu,链接速度和其他一些信息。我为此使用udev。我已经弄清楚了如何获取mac地址和mtu,但还没有弄清楚链接速度。我可以从终端使用ethtool来获取它,但是我需要一种以编程方式获取它的方法。 有谁知道我如何获得udev或其他库的链接速度属性? 问题答案: 您需要使用ioctl()调用。在LinuxJourna

  • 问题内容: 我想从受监视的应用程序内部以编程方式获得等效的输出。我看到可以通过HotSpot诊断Bean触发二进制堆转储,但是我看不到如何获取直方图数据。可能吗 ? 问题答案: 这可能是不是最好的例子/代码,但看看这个 (我认为这仅适用于Hotspot JVM)

  • 问题内容: 我需要使用Java获取我的android设备的MAC地址。我已经在网上搜索过,但没有发现任何有用的信息。 问题答案: 正如评论中已经指出的那样,可以通过接收MAC地址。 同样不要忘记在你的计算机中添加适当的权限

  • 问题内容: 基本上,我想获取python解释器的句柄,以便可以传递脚本文件来执行(从外部应用程序执行)。 问题答案: 这适用于Linux和Windows: Python 3.x Python 2.x

  • 我在Android中初始化了Firebase,而没有使用像这样的google-service插件 现在,在初始化firebase应用程序之后,我如何获得生成的令牌来通过FCM发送推送通知