【问题标题】:Use Google Sheets Java API with API key not OAuth?使用带有 API 密钥而不是 OAuth 的 Google Sheets Java API?
【发布时间】:2019-05-22 21:28:17
【问题描述】:

有没有办法将“Google Sheet Java API”与 API 密钥一起使用,而不是在他们的示例中给出的 OAuth

https://developers.google.com/sheets/api/quickstart/java

我知道您可以使用 HTTP 请求通过 API 密钥获取数据,但我在想是否有办法使用 google 提供的 Java API 来执行此操作,这样我就不必为每个请求解析 JSON。

【问题讨论】:

  • 你只能使用API​​ key来消费数据——如果你想,你需要使用OAuth

标签: java google-sheets google-sheets-api


【解决方案1】:

我没有找到任何官方方法来实现这一点,但我能够按照Acquiring and using an API key 中的描述做到这一点:

获得 API 密钥后,您的应用程序可以将查询参数 key=yourAPIKey 附加到所有请求 URL。

通过使用请求拦截器并手动添加key 查询参数,如下所示:

private Sheets getSheets() {
    NetHttpTransport transport = new NetHttpTransport.Builder().build();
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer httpRequestInitializer = request -> {
        request.setInterceptor(intercepted -> intercepted.getUrl().set("key", API_KEY));
    };

    return new Sheets.Builder(transport, jsonFactory, httpRequestInitializer)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

public List<List<Object>> getValues(String spreadsheetId, String range) throws IOException {
    return getSheets()
            .spreadsheets()
            .values()
            .get(spreadsheetId, range)
            .execute()
            .getValues();
}

【讨论】:

    【解决方案2】:

    是的,你可以,基本上你需要类似以下的东西:

    public NetHttpTransport netHttpTransport() throws GeneralSecurityException, IOException {
        return GoogleNetHttpTransport.newTrustedTransport();
    }
    
    public JacksonFactory jacksonFactory() {
        return JacksonFactory.getDefaultInstance();
    }
    
    private Set<String> googleOAuth2Scopes() {
        Set<String> googleOAuth2Scopes = new HashSet<>();
        googleOAuth2Scopes.add(SheetsScopes.SPREADSHEETS_READONLY);
        return Collections.unmodifiableSet(googleOAuth2Scopes);
    }
    
    public GoogleCredential googleCredential() throws IOException {
        File serviceAccount = new ClassPathResource("serviceAccount.json").getFile();
        return GoogleCredential.fromStream(new FileInputStream(serviceAccount))
                .createScoped(googleOAuth2Scopes());
    }
    
    public Sheets googleSheets() {
        return new Sheets(netHttpTransport(), jacksonFactory(), googleCredential());
    }
    

    您可以在此处阅读有关serviceAccount.json 的更多信息:https://cloud.google.com/iam/docs/creating-managing-service-account-keys


    以上内容来自我与 Google API 集成的一个示例 Spring Boot 项目:https://github.com/ciscoo/spring-boot-google-apis-example

    【讨论】:

    • 这不还是OAuth而不是API Key认证吗?仅使用服务帐户而不是用户帐户。
    【解决方案3】:

    您也可以使用此代码:

    Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                    .setApplicationName(APPLICATION_NAME)
                    .setGoogleClientRequestInitializer(CommonGoogleClientRequestInitializer.newBuilder().setKey(API_KEY).build())
                    .build();
    

    【讨论】:

      猜你喜欢
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      相关资源
      最近更新 更多