我正在尝试通过官方 Google .NET 库使用 GMail API。我正在使用 razor 页面构建 ASP .NET 8 应用程序。我设法让用户登录并将令牌存储在数据库中。我还可以从数据库检索令牌,并根据我存储的刷新令牌获取新的访问令牌。但是,当我尝试检索消息时,我收到“BAD Request”响应:
{
"error": {
"code": 400,
"message": "Precondition check failed.",
"errors": [
{
"message": "Precondition check failed.",
"domain": "global",
"reason": "failedPrecondition"
}
],
"status": "FAILED_PRECONDITION"
}
}
首先我请求一个新的访问令牌:
https://oauth2.googleapis.com/token
User-Agent = google-api-dotnet-client/1.68.0.0; User-Agent = (gzip)
refresh_token=***&grant_type=refresh_token&client_id=***.apps.googleusercontent.com&client_secret=***
哪个成功:
{
"access_token": "***",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email",
"token_type": "Bearer",
"id_token": "***"
}
之后我尝试请求消息:
https://gmail.googleapis.com/gmail/v1/users/me/messages
User-Agent = ConsographKengraAdapterGoogle; User-Agent = google-api-dotnet-client/1.68.0.0; User-Agent = (gzip); x-goog-api-client = gl-dotnet/8.0.0 gdcl/1.68.0.3399; Authorization = Bearer ***
由于上述错误响应而失败:
{
"error": {
"code": 400,
"message": "Precondition check failed.",
"errors": [
{
"message": "Precondition check failed.",
"domain": "global",
"reason": "failedPrecondition"
}
],
"status": "FAILED_PRECONDITION"
}
}
我不确定可能是什么问题。
您的请求格式正确吗?