简体   繁体   English

GCloud App Engine(灵活)默认服务帐户 Scope

[英]GCloud App Engine (flexible) Default Service Account Scope

I'm trying to connect to a google calendar from service running in GCP App Engine flexible environment using default service account.我正在尝试使用默认服务帐户从 GCP App Engine 灵活环境中运行的服务连接到谷歌日历。 I have set correct scope required to access the calendar to read only events ( https://www.googleapis.com/auth/calendar.events.readonly ) and I'm able to access the calendar locally when impersonating the service account.我已经设置了正确的 scope 访问日历所需的只读事件( https://www.googleapis.com/auth/calendar.events.readonly )并且我能够在模拟服务帐户时在本地访问日历。

My service runs on java spring boot and getting below error我的服务在 java spring 启动时运行并出现以下错误

{
  "message": "Request had insufficient authentication scopes.",
  "status": "PERMISSION_DENIED",
  "details": [
  {
    "@type": "type.googleapis.com/google.rpc.ErrorInfo",
    "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
    "domain": "googleapis.com",
    "metadata": {
      "service": "calendar-json.googleapis.com",
      "method": "calendar.v3.Events.List"
    }
  }
}

Code Snippet代码片段

val scopes = listOf("https://www.googleapis.com/auth/calendar.events.readonly")
val credentialsProvider = GoogleCredentialsProvider.newBuilder().setScopesToApply(scopes).build()

val calendarService = Calendar.Builder(GoogleNetHttpTransport.newTrustedTransport(),
                                GsonFactory.getDefaultInstance(),
                                HttpCredentialsAdapter(credentialsProvider.credentials))
            .setApplicationName(applicationName)
            .build()

calendarService.events().list(config.calendarId)
            .setSingleEvents(true)
            .setTimeMin(DateTime(Date()))
            .setMaxResults(4)
            .execute()

Dependencies依赖项

  • com.google.cloud:spring-cloud-gcp-dependencies:3.1.0 com.google.cloud:spring-cloud-gcp-dependencies:3.1.0
  • org.springframework.cloud:spring-cloud-dependencies:2021.0.1 org.springframework.cloud:spring-cloud-dependencies:2021.0.1
  • com.google.cloud:spring-cloud-gcp-starter com.google.cloud:spring-cloud-gcp-starter
  • com.google.auth:google-auth-library-appengine com.google.auth:google-auth-library-appengine
  • com.google.appengine:appengine-api-1.0-sdk com.google.appengine:appengine-api-1.0-sdk
  • com.google.apis:google-api-services-calendar:v3-rev411-1.25.0 com.google.apis:google-api-services-calendar:v3-rev411-1.25.0

What I've Tried我试过的

I have accessed the metadata endpoint on app instance directly to check provided token.我直接访问了应用程序实例上的元数据端点以检查提供的令牌。 Only to find out the given token has no specified calendar scope.只找出给定的令牌没有指定的日历 scope。

$ curl -s "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.events.readonly" -H 'Metadata-Flavor: Google'
{"access_token":"ya29.xxxxxx....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................","expires_in":3518,"token_type":"Bearer"}

$ curl -H "Content-Type: application/x-www-form-urlencoded" -d "access_token=$token" https://www.googleapis.com/oauth2/v1/tokeninfo
{
  "issued_to": "xxxxxxx",
  "audience": "xxxxxxx",
  "scope": "https://www.googleapis.com/auth/logging.write https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/devstorage.full_control https://www.googleapis.com/auth/appengine.apis",
  "expires_in": 3493,
  "email": "<project-id>@appspot.gserviceaccount.com",
  "verified_email": true,
  "access_type": "online"
}

Google Compute Engine instance can have the permissions of the service account limited by access scopes . Google Compute Engine 实例可以拥有受访问范围限制的服务帐户的权限。

The scopes that you list are the default access scopes link .您列出的范围是默认访问范围链接 This means the scopes you add are filtered out by the VM instance settings.这意味着您添加的范围会被 VM 实例设置过滤掉。

In the Google Cloud Console GUI, change the Access scopes for the VM instance to be Allow full access to all Cloud APIs .在 Google Cloud Console GUI 中,将 VM 实例的访问范围更改为Allow full access to all Cloud APIs You can also use the CLI link .您也可以使用 CLI 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM