How to Select Files in Android Application with 'drive.file' Scope

158 Views Asked by At

I am currently working on the development of a full native Android application and have received communication from Google to transition from the restricted 'auth/drive' scope to the less restrictive 'auth/drive.file' scope.

During this migration, I have two questions:

  1. How can users access files or folders with the 'drive.file' scope in a full native Android app, excluding the use of the Google Picker API?
    I encountered a '404 File not found' error while trying to retrieve a folder in my development environment. Is there a workaround for this issue without changing the scope?

  2. If the first method is not feasible, how can the Google Picker API be used on Android?
    The documentation indicates that, for Android, the use of the Google Workspace API is the only option. In this case, is it possible to achieve functionality similar to selecting Google Drive files using the Google Picker API through methods other than the Google Workspace API?
    Link to Google Picker API Documentation

I've been working on transitioning from the 'auth/drive' scope to the less restrictive 'drive.file' scope in a native Android application. Here are the steps I've taken and the issue I'm facing:

  1. Obtained a refresh token with the 'drive.file' scope by making changes in the authorization process and acquiring an access token from https://accounts.google.com/o/oauth2/token.

  2. Sent a request to retrieve the list of folders using GET /drive/v3/files, but the response's 'items' field is empty.

  3. Attempted to access the root folder by sending a request with GET /drive/v3/files/{fileId}, specifying the fileId of the root folder. However, a '404 File not found' error was returned, indicating that access to the folder is not possible.

I followed the official documentation for the Google Drive API

Code for sending a request for GET /drive/v3/files/{fileId}:

// Header
Header[] requestHeaders = new Header[]{
        // Access token already obtained from post https://accounts.google.com/o/oauth2/token
        new BasicHeader("Authorization", accessToken),
        new BasicHeader("Cache-Control", "no-cache"),
        new BasicHeader("Pragma", "no-cache"),
        new BasicHeader("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"),
        new BasicHeader("Connection", "keep-alive")
};

InputStream inputStream = null;
try {
    // Perform a GET request to retrieve data
    setHttpResponse(
            doGet(httpClient, "https://www.googleapis.com/drive/v3/files/{fileId}", requestHeaders)
    );
    int status = getHttpResponse().getStatusLine().getStatusCode();

    // setting response data
    inputStream = getHttpResponse().getEntity().getContent();
    String responseContentType = GoogleDriveUtils.getResponseContentType(this.getHttpResponse());

    // Error determination
    // Status other than 200 or Content-Type other than "application/json"
    if (status != 200 || !GoogleDriveUtils.isContentTypeJson(responseContentType)) {
        // exception 
        throw GoogleDriveUtils.createGoogleDriveException(
                fulUrl, inputStream, status, responseContentType,
                this.getHttpResponse(), GoogleDriveConsts.PROCESS_TYPE_RETRIEVAL);
    }

    // Response Analysis
    googleDriveAboutInfo = GoogleDriveAboutInfo.parseGoogleDriveAboutInfo(inputStream);

} 

Here is the error message received:

{
  "error": {
    "code": 404,
    "message": "File not found: {FileID}",
    "errors": [
      {
        "message": "File not found: {FileID}",
        "domain": "global",
        "reason": "notFound",
        "location": "file",
        "locationType": "other"
      }
    ]
  }
}
0

There are 0 best solutions below