google-cloud-platform - Google API 0Auth2 无法检索访问令牌
问题描述
我是 Google API 的新手。我有一个脚本可以将所有邮件导入 Google 网上论坛,但我无法让 API 正常工作。我有我的 client_id,client_secret
然后我使用了这个链接:
https://accounts.google.com/o/oauth2/auth?client_id=[CLIENID]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/apps.groups.migration&response_type=code
我用我的 ClientID 替换了 [CLIENDID],我可以进行身份验证并取回 AuthCode,然后我用它来运行这个命令:
curl --request POST --data "code=[AUTHCODE]&client_id=[CLIENTID]&client_secret=[CLIENTSECRET]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
这有效并向我显示了刷新令牌,但是,脚本确实说身份验证失败的。所以我尝试再次运行该命令,它说
“错误”:“invalid_grant”
"error_description": "错误请求"
如果我重新打开上面的链接,获取一个新的 authcode 并再次运行该命令,它可以工作,但只是第一次。我使用的是 NPO Google 帐户并激活了试用期。
有谁可以帮我离开这里吗?
完整脚本:
client_id="..."
client_secret="...."
refresh_token="......"
function usage() {
(
echo "usage: $0 <group-address> <mbox-dir>"
) >&2
exit 5
}
GROUP="$1"
shift
MBOX_DIR="$1"
shift
[ -z "$GROUP" -o -z "$MBOX_DIR" ] && usage
token=$(curl -s --request POST --data "client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token&grant_type=refresh_token" https://accounts.google.com/o/oauth2/token | sed -n "s/^\s*\"access_token\":\s*\"\([^\"]*\)\",$/\1/p")
# create done folder if it doesn't already exist
DONE_FOLDER=$MBOX_DIR/../done
mkdir -p $DONE_FOLDER
i=0
for file in $MBOX_DIR/*; do
echo "importing $file"
response=$(curl -s -H"Authorization: Bearer $token" -H'Content-Type: message/rfc822' -X POST "https://www.googleapis.com/upload/groups/v1/groups/$GROUP/archive?uploadType=media" --data-binary @${file})
result=$(echo $response | grep -c "SUCCESS")
# check to see if it worked
if [[ $result -eq 0 ]]; then
echo "upload failed on file $file. please run command again to resume."
exit 1
fi
# it worked! move message to the done folder
mv $file $DONE_FOLDER/
((i=i+1))
if [[ $i -gt 9 ]]; then
expires_in=$(curl -s "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=$token" | sed -n "s/^\s*\"expires_in\":\s*\([0-9]*\),$/\1/p")
if [[ $expires_in -lt 300 ]]; then
# refresh token
echo "Refreshing token..."
token=$(curl -s --request POST --data "client_id=$client_id&client_secret=$client_secret&refresh_token=$refresh_token&grant_type=refresh_token" https://accounts.google.com/o/oauth2/token | sed -n "s/^\s*\"access_token\":\s*\"\([^\"]*\)\",$/\1/p")
fi
i=0
fi
done
解决方案
推荐阅读
- spring-boot - Spring Security + AAD:invalid_token_response 跟进最新可用的 spring-boot 版本(2.1.2)
- java - 带有泛型的 Java 继承
- javascript - Fabric JS - 移动、缩放和旋转时在自定义边界外剪裁文本
- r - 根据两个条件填充 geom_point():在列值上,或者如果值为 0
- c++ - 使用 GetDefaultCommConfig 时出现无效参数错误
- html - 添加 div 导致图像消失
- python - How to add an \n in txt when meet Specified word in python
- python-3.x - Python - 根据另一个数据集的列值过滤数据集
- vim - 在vim中,如何突出显示用户定义的数据类型?
- mysql - 如何在具有逗号分隔的 int 值的列上使用 group_concat 时获得唯一的结果?