java - 使用带有 API 密钥而不是 OAuth 的 Google Sheets Java API?
问题描述
有没有办法将“Google Sheet Java API”与 API 密钥一起使用,而不是在他们的示例中给出的 OAuth
https://developers.google.com/sheets/api/quickstart/java
我知道您可以使用 HTTP 请求通过 API 密钥获取数据,但我在想是否有办法使用 google 提供的 Java API 来执行此操作,这样我就不必为每个请求解析 JSON。
解决方案
我没有找到任何官方方法来实现这一点,但我能够按照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();
}
推荐阅读
- gradle - Gradle:启动任务时无法转义特殊字符
- python - 在 Colab 中使用 CUDA
- sql - 我想从最近生效日期的重复值中发送整条记录
- c# - 我怎样才能正确地“装箱”我的字符串,这样它就不会消失?
- wordpress - 需要帮助在 WooCommerce 产品中显示一些元数据
- javascript - 设置状态后音频未正确更新
- c# - Bootstrap 4.5 选项卡始终打开页面而不是 Blazor 中的选项卡内容
- javascript - 在众多模块中重新分配变量?
- javascript - 未捕获的 ReferenceError:react.DOM.render 中未定义 require$$2
- flutter - 参数类型“字符串?” 无法将参数类型分配给颤振中的“字符串”错误