(Android/Java)Google表格v4 isUserRecoverableError状态:NEED_PERMISSION
我目前正试图使用Google Sheets v4 API简单地删除我的Google Spreadsheet中的一行。(Android/Java)Google表格v4 isUserRecoverableError状态:NEED_PERMISSION
这里是我使用的代码:
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的快速启动提供的一个排序誓言证书的那我需要包括?
从给定的JSON响应基础,出现的错误是由于认证范围不足。您可以尝试检查Authorizing requests with OAuth 2.0中给出的Google Sheets API所需的OAuth 2.0范围信息。
请注意,针对非公开用户数据的Google Sheets API请求必须由经过身份验证的用户授权。同样,如果应用程序需要创建电子表格或以其他方式处理元数据,那么应用程序还必须请求Google Drive API范围。
此外,本SO后给出的解决方案 - 403 Forbidden error when accessing Google Drive API downloadURL关于错误代码403也可能有所帮助。
由于没有一个关于与Android应用程序管理表单的问题,这里是从头请求额外OATH 2.0的方式,这我花了几个小时就明白了:
战略,以获得右附加授权... https://developers.google.com/identity/protocols/OAuth2InstalledApp#libraries 图:
所有这些请求都可以用内的AsyncTask进口一些诸如HttpURLConnection的...来处理。
范围列表... https://developers.google.com/sheets/guides/authorizing
问题很可能是您加载的存储凭证的范围不足,您需要删除存储的凭据并再次授权访问。
您的代码似乎是从Java quickstart改编的,该代码请求只读范围。试图使用该范围进行写入操作将失败,并出现此错误。
您是否在范围之外还包含有效的oauth凭证? (另外,由by开始的范围是包含的,并且结束范围是独占的,因此要求删除[14,14]将最终删除任何内容。如果你想删除第14行,你想要[14,15]。) –
谢谢你的回复Sam。其余代码仅仅是示例快速入门... mCredential = GoogleAccountCredential.usingOAuth2( getApplicationContext(),Arrays.asList(SCOPES)) 。setBackOff(new ExponentialBackOff());我不确定如何包含另一个有效的宣誓证书......我需要某种API密钥吗? – LinearM