【发布时间】:2016-08-17 03:35:22
【问题描述】:
我目前正在尝试使用 Google Sheets v4 API 从我的 Google 电子表格中删除一行。
这是我正在使用的代码:
private void deleteRow()
{
List<Request> requests = new ArrayList<>();
DeleteDimensionRequest deleteDimensionRequest = new DeleteDimensionRequest();
DimensionRange dimensionRange = new DimensionRange();
dimensionRange.setStartIndex(14);
dimensionRange.setEndIndex(15);
deleteDimensionRequest.setRange(dimensionRange);
requests.add(new Request()
.setDeleteDimension(deleteDimensionRequest)
);
BatchUpdateSpreadsheetRequest batchUpdateRequest = new BatchUpdateSpreadsheetRequest()
.setRequests(requests);
try
{
mService.spreadsheets().batchUpdate("Spreadsheet_ID", batchUpdateRequest).execute();
}
catch(IOException e)
{
e.printStackTrace();
}
}
这个函数给我的错误是:
08-14 15:47:10.818 26956-27285/com.xxx.xxxxx.xxxxxxxx
W/GoogleAuthUtil: isUserRecoverableError status: NEED_PERMISSION
在我的其他类文件中,我已经指出了权限范围,包括驱动器和电子表格。
在 java 快速入门中...
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
SheetsQuickstart.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
除了我需要包含的 android 快速入门提供的证书之外,这是一种宣誓证书吗?
【问题讨论】:
-
除了范围之外,您是否还包括有效的 oauth 凭据? (另外,通过,开始范围是包含的,结束范围是排除的。所以要求删除 [14,14) 最终不会删除任何内容。如果要删除第 14 行,则需要 [14,15)。)
-
感谢山姆的回复。其余代码只是示例快速入门... mCredential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(SCOPES)) .setBackOff(new ExponentialBackOff());我不确定如何包含另一个有效的宣誓证书...我需要某种 API 密钥吗?
标签: java android google-sheets-api