Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform Google API 0Auth2无法检索访问令牌_Google Cloud Platform_Google Oauth - Fatal编程技术网

Google cloud platform Google API 0Auth2无法检索访问令牌

Google cloud platform Google API 0Auth2无法检索访问令牌,google-cloud-platform,google-oauth,Google Cloud Platform,Google Oauth,我是谷歌API的新手。我有一个脚本可以将所有邮件导入谷歌群组,但我无法让API正常工作。 我有我的客户id,客户机密 然后我使用了这个链接: 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

我是谷歌API的新手。我有一个脚本可以将所有邮件导入谷歌群组,但我无法让API正常工作。 我有我的客户id,客户机密

然后我使用了这个链接:
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
在我将[CLIENDID]替换为我的ClientID的地方,我可以进行身份验证并获取用于运行此命令的AuthCode:
curl--request POST--data“code=[AUTHCODE]&client\u id=[CLIENTID]&client\u secret=[CLIENTSECRET]&redirect\u uri=urn:ietf:wg:oauth:2.0:oob&grant\u type=authorization\u code”https://accounts.google.com/o/oauth2/token
这可以工作并向我显示刷新令牌,但是,脚本确实说身份验证失败。所以我试着再次运行命令,它说

“错误”:“无效的授权”

“错误描述”:“错误请求”

如果我重新打开上面的链接,获取一个新的authcode并再次运行该命令,它将工作,但只会第一次工作。我在一个非营利组织的谷歌帐户上,我激活了试用期

有人能帮我吗

完整脚本:


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

客户端id=“…”
客户_secret=“…”
刷新令牌=“……”
函数用法(){
(
echo“用法:$0”
) >&2
出口5
}
GROUP=“$1”
转移
MBOX_DIR=“$1”
转移
[-z“$GROUP”-o-z“$MBOX_DIR”]&用法
令牌=$(curl-s--请求POST--数据“客户端\u id=$client\u id&client\u secret=$client\u secret&refresh\u令牌=$refresh\u令牌&grant\u type=refresh\u令牌”https://accounts.google.com/o/oauth2/token |sed-n“s/^\s*\”访问令牌\“:\s*\”\([^\“]*\)\”,$/\1/p”)
#如果“完成”文件夹不存在,请创建该文件夹
完成\文件夹=$MBOX\ U目录/。/完成
mkdir-p$DONE_文件夹
i=0
对于$MBOX_DIR/*;do中的文件
echo“导入$file”
响应=$(curl-s-H“授权:承载者$token”-H'Content-Type:message/rfc822'-X POST”https://www.googleapis.com/upload/groups/v1/groups/$GROUP/archive?uploadType=media”--数据二进制@${file})
结果=$(echo$响应| grep-c“成功”)
#检查它是否有效
如果[[$result-等式0]];则
echo“文件$file的上载失败。请再次运行命令以继续。“
出口1
fi
#成功了!将邮件移动到“完成”文件夹
mv$file$DONE\u文件夹/
((i=i+1))
如果[$i-gt 9]];则
expires_in=$(curl-s)https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=$token“|sed-n”s/^\s*\”在\”中过期\“:\s*\([0-9]*\),$/\1/p”)
如果[[$expires_in-lt 300]];则
#刷新令牌
echo“刷新令牌…”
令牌=$(curl-s--请求POST--数据“客户端\u id=$client\u id&client\u secret=$client\u secret&refresh\u令牌=$refresh\u令牌&grant\u type=refresh\u令牌”https://accounts.google.com/o/oauth2/token |sed-n“s/^\s*\”访问令牌\“:\s*\”\([^\“]*\)\”,$/\1/p”)
fi
i=0
fi
完成

我写了一篇文章,展示了如何使用
curl
实现Google OAuth 2@JohnHanley很棒,非常感谢分享:)我写了一篇文章,展示了如何使用
curl
实现GoogleOAuth2@非常好,非常感谢分享:)