简体   繁体   中英

(Android/Java) Google Sheets v4 isUserRecoverableError status: NEED_PERMISSION

I am currently trying to simply delete a row off my Google Spreadsheet using the Google Sheets v4 API.

Here is the code I am using:

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();
    }
}

The error this function gives me is:

08-14 15:47:10.818 26956-27285/com.xxx.xxxxx.xxxxxxxx         
W/GoogleAuthUtil: isUserRecoverableError status: NEED_PERMISSION

In my other class file, I've already indicated the scopes of permissions including drive and spreadsheets.

Here is the picture of the error: 在此处输入图片说明

In the java quickstart...

    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;
}

Is this the sort of oath credential in addition to the one provided by the android quickstart that I need to include?

Based from the given JSON response, encountered error is due to insufficient authentication scope. You can try to check required OAuth 2.0 scope information for the Google Sheets API as given in Authorizing requests with OAuth 2.0 .

Please note that requests to the Google Sheets API for non-public user data must be authorized by an authenticated user. Likewise, if an application needs to create spreadsheets, or otherwise manipulate their metadata, then the application must also request a Google Drive API scope.

Additionally, the solution given in this SO post - 403 Forbidden error when accessing Google Drive API downloadURL regarding error code 403 might also help.

The problem is likely that you are loaded stored credentials with the insufficient scopes, and you need to delete your stored credentials and authorize access again.

You code appears to be adapted from the Java quickstart , which requests a readonly scope. Trying to use that scope for a write operation would fail with this error.

Since there is not one single question relating to managing sheets with an android application, here is the way to request an additional OATH 2.0 from scratch, which took me a few hours to understand:

Strategy to get the right additional authorization... https://developers.google.com/identity/protocols/OAuth2InstalledApp#libraries Diagram: 在此处输入图片说明

All of these requests can be handled with some imports such as HttpUrlConnection... within AsyncTask.

The list of scopes... https://developers.google.com/sheets/guides/authorizing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM