如何使用 Go Google SDK 模拟用户?

How to impersonate user using Go Google SDK?

我有一个用户用来查询 Reports API. I generated the token and read the credentials using the google.ConfigFromJSON 方法。

虽然,现在我需要做同样的事情,但我需要使用服务帐户而不是用户。根据文档,我需要模拟用户,因为无法使用服务帐户调用 API(如果我错了请纠正我)。

这是我模拟用户所做的:

impersonatedOption := option.ImpersonateCredentials("user@project.iam.gserviceaccount.com")
credsOption := option.WithCredentialsFile("cert.json")
scopesOption := option.WithScopes(admin.AdminReportsAuditReadonlyScope)

httpClient, _, err := transport.NewHTTPClient(ctx,  scopesOption, credsOption, impersonatedOption)

srv, err := admin.NewService(ctx, option.WithHTTPClient(httpClient))

但没有成功:

Get "https://admin.googleapis.com/admin/reports/v1/activity/users/all/applications/login?alt=json&eventName=account_disabled_spamming&prettyPrint=false": impersonate: status code 403: 
{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED"
  }
}

服务帐户配置为超级管理员,应具有所有权限。

我只需要加载证书,并将 Subject 设置为能够模拟用户:

    config, err := google.JWTConfigFromJSON(b, scope...)
    config.Subject = "impersonated_user@email.com"