使用带有API密钥的Google Sheets Java API而不是OAuth?

w46czmvw  于 2022-12-10  发布在  Java
关注(0)|答案(4)|浏览(168)

有没有办法使用“Google Sheet Java API”与API密钥,而不是与OAuth,这是在他们的例子
https://developers.google.com/sheets/api/quickstart/java
我知道你可以使用HTTP请求来获取带有API密钥的数据,但我在想是否有一种方法可以使用谷歌提供的Java API来实现这一点,这样我就不必为每个请求解析JSON。

w51jfk4q

w51jfk4q1#

我没有找到任何官方的方法来实现这一点,但我能够做到这一点,如获取和使用API密钥中所述:
获得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();
}
iyzzxitl

iyzzxitl2#

是的,你可以,基本上你需要像下面这样:

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集成的一个示例Sping Boot 项目:https://github.com/ciscoo/spring-boot-google-apis-example

bhmjp9jg

bhmjp9jg3#

这是可能的。下面是我如何使用API密钥而不是OAuth初始化Sheets的示例:

SheetsRequestInitializer REQUEST_INITIALIZER = new SheetsRequestInitializer(API_KEY);
JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Sheets service = new Sheets.Builder(httpTransport, JSON_FACTORY, null)
            .setApplicationName("HelloWorld")
            .setGoogleClientRequestInitializer(REQUEST_INITIALIZER)
            .build();
dy1byipe

dy1byipe4#

您还可以使用以下代码:

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

相关问题