列出Google云端硬盘中所有文件的脚本:Api,cURL和Bash [英] Script to list all files in Google Drive : Api, cURL and Bash

查看:109
本文介绍了列出Google云端硬盘中所有文件的脚本:Api,cURL和Bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用cURL的bash脚本,用于列出我在Google云端硬盘帐户中的所有文件(不是与我共享的文件,而是我自己的文件)。您必须在帐户中授予对Google Drive API的访问权限,并在脚本中填充变量 $ idclient $ idsecret

Here is a bash script using cURL to list all my files from a Google Drive account (not the shared with me files, only my own files). You will have to grant access to the Google Drive API in your account and populate, in the script, the variable $idclient and $idsecret :

#!/bin/bash

idclient="Write your ID client between double quote"
idsecret=Write your ID secret here, with no quote

firefox "https://accounts.google.com/o/oauth2/auth?client_id="${idclient}"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/drive&response_type=code"

echo "Paste here the code that Google gave you: "
read apptoken

curl --data client_id="${idclient}" --data client_secret="${idsecret}" --data code="${apptoken}" --data redirect_uri=urn:ietf:wg:oauth:2.0:oob --data grant_type=authorization_code https://www.googleapis.com/oauth2/v4/token > curlID.txt
accesstoken=$(cat curlID.txt | grep 'access_token' | cut -c20-154)
refreshtoken=$(cat curlID.txt | grep refresh_token | cut -c21-123)
cat curlID.txt
rm curlID.txt 

curl  'https://www.googleapis.com/drive/v3/files?pageSize=1000&q=%27me%27%20in%20owners&key="'$idclient'"'  --header 'Authorization: Bearer "'$accesstoken'"' --header 'Accept: application/json'  --compressed > listefichiers_temp.txt

nextPageToken=$(grep nextPageToken listefichiers_temp.txt | sed 's/"nextPageToken": "//' | sed 's/.$//' | sed 's/.$//' )

echo
echo IDCLIENT :
echo $idclient
echo
echo IDSECRET :
echo $idsecret
echo
echo APPTOKEN :
echo $apptoken
echo
echo ACCESS_TOKEN:
echo $accesstoken
echo
echo REFRESH_TOKEN
echo $refreshtoken
echo
echo NEXT_PAGE_TOKEN:
echo $nextPageToken
echo

cat listefichiers_temp.txt | grep '"id"' | cut -c 11- | sed 's/.$//' | sed 's/.$//' >> listefichiers.txt
touch log1.txt
cat listefichiers_temp.txt >> log1.txt

### LOOP ###

while grep nextPageToken listefichiers_temp.txt; do
    nextPageToken=$(grep nextPageToken listefichiers_temp.txt | sed 's/"nextPageToken": "//' | sed 's/.$//' | sed 's/.$//' | cut -c 2- )
    echo NEXT_PAGE_TOKEN
    echo $nextPageToken
    echo
    rm listefichiers_temp.txt
    curl \
       'https://www.googleapis.com/drive/v3/files?pageSize=1000&pageToken='$nextPageToken'&q=%27me%27%20in%20owners&key='$idclient'' \
       --header 'Authorization: Bearer '$accesstoken'' \
       --header 'Accept: application/json' \
       --compressed > listefichiers_temp.txt
    cat listefichiers_temp.txt >> log1.txt
    cat listefichiers_temp.txt | grep '"id"' | cut -c 11- | sed 's/.$//' | sed 's/.$//' >> listefichiers.txt
    echo
    echo Nombre de fichiers
    cat listefichiers.txt | wc -l
    echo
    nextPageToken=
done

date
echo Nombre de fichiers
cat listefichiers.txt | wc -l


推荐答案

最初,我的脚本无法正常工作。 $ nextTokenPage 的开头有一个多余的空间,因此我在正确的位置添加了一个 cut 来删除此错误的空间。使用 sh -xe script.sh 查看脚本的输出时,我能够对问题进行故障排除。

Initially, my script was not working. There was an unwanted space at the beginning of $nextTokenPage so I add a cut at the right place to remove this erroneous space. I was able to troubleshoot the issue while viewing an output of the script with sh -xe script.sh.

这篇关于列出Google云端硬盘中所有文件的脚本:Api,cURL和Bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆