You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GKE中Scala调用Google Services API遇ACCESS_TOKEN_SCOPE_INSUFFICIENT问题

GKE环境下Scala调用Google Services API权限问题

我在GKE环境中使用Scala调用Google Drive API创建电子表格时遇到权限错误,示例代码如下:

object GoogleDrive {
    def createSpreadSheet(name: String, folder: String): String = {
        val file = new File()
            .setParents(List(folder).asJava)
            .setMimeType("application/vnd.google-apps.spreadsheet")
            .setName(name)
        s"https://docs.google.com/spreadsheets/d/${service.files().create(file).setFields("id").execute().getId()}/"
    }

    private lazy val service = authorize()
    private def authorize(): Drive = {
        val httpTransoport = GoogleNetHttpTransport.newTrustedTransport()
        val jsonFactory = GsonFactory.getDefaultInstance()
        val credential = GoogleCredential.getApplicationDefault(httpTransoport, jsonFactory).createScoped(List(DriveScopes.DRIVE_FILE).asJava)
        credential.refreshToken()

        new Drive.Builder(httpTransoport, jsonFactory, credential).setApplicationName("whatever-app").build()
    }
}

运行时抛出ACCESS_TOKEN_SCOPE_INSUFFICIENT错误,但通过curl进入Pod手动请求流程完全正常,说明GKE侧的权限配置是正确的。

通过tcpdump抓包排查发现:代码请求token时没有携带scope参数——手动curl请求的URL是GET /computeMetadata/v1/instance/service-accounts/default/token?scope=https://www.googleapis.com/auth/drive.file,但Scala代码发送的请求只有GET /computeMetadata/v1/instance/service-accounts/default/token(另外,创建GoogleCredential时如果不设置scope会直接崩溃)。

我已经尝试了多个google-api-client版本,包括2.2.0(该版本需要移除Drive相关代码才能运行),但所有版本的代码请求token时都不会带上scope参数。

请问这是我的代码操作有误,还是需要在GKE基础设施层面配置scope?注:实际业务中会涉及更多Drive和Sheets的复杂操作,手动发送HTTP请求的方案不可行。

内容的提问来源于stack exchange,提问作者Kegluneq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 05:05:20