Google Analytics:尝试进行授权 API 调用时 JWT 签名 (invalid_grant) 无效

Bo.*_*Bo. 8 rest google-analytics oauth-2.0 jwt

我尝试对 Google Analytics 中的服务帐户进行授权 API 调用(HTTP/REST)。使用此文档:https ://developers.google.com/identity/protocols/OAuth2ServiceAccount

我只是使用 HTTP/REST 请求来测试。

所以我有服务帐户的私钥文件:

{
  "type": "service_account",
  "project_id": "test-x",
  "private_key_id": "some_private_key_id",
  "private_key": "-----BEGIN PRIVATE KEY----- some_private_key -----END PRIVATE KEY-----",
  "client_email": "test-01@test-x.iam.gserviceaccount.com",
  "client_id": "some_client_id",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-01%40test-x.iam.gserviceaccount.com"
}
Run Code Online (Sandbox Code Playgroud)

我根据 client_email 创建 JWT:test-01@test-x.iam.gserviceaccount.com

标题:

{"alg":"RS256","typ":"JWT"}
Run Code Online (Sandbox Code Playgroud)

索赔集:

{
  "iss": "test-01@test-x.iam.gserviceaccount.com",
  "scope": "https://www.googleapis.com/auth/analytics.readonly",
  "aud": "https://www.googleapis.com/oauth2/v4/token",
  "exp": 1488820112,
  "iat": 1488816522
}
Run Code Online (Sandbox Code Playgroud)

iat - 我刚刚设置了当前

exp - 当前 + 1 小时,

我使用此服务来创建签名: https: //jwt.io/#debugger

它生成编码值,我尝试将其用于访问令牌请求

当我尝试使用“编码”字段生成的结果时:

curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=JWT_that_has_been_signed' https://www.googleapis.com/oauth2/v4/token
Run Code Online (Sandbox Code Playgroud)

结果:

{
 "error": "invalid_grant",
 "error_description": "Invalid JWT Signature."
}
Run Code Online (Sandbox Code Playgroud)

但我不使用我的私钥。根据计算签名的文档,我必须使用我的私钥。我不完全理解如何以正确的方式使用密钥来正确计算签名。

jwt.io 已经生成公钥和私钥...

可能我错误地使用了 jwt.io..

请告诉我创建 JWT 的正确方法,或者可能是其他服务来创建它。

谢谢!

Nad*_*smi 0

我遇到了同样的问题,我只是创建了一个新的私钥google cloud platform

  IAM & admin -> Service accounts 
Run Code Online (Sandbox Code Playgroud)

Service accounts for project项目名

已经创建了您的服务帐户,单击actions-> Create Key

并更新您项目的私钥。希望它能帮助你。