Get the list of all spreadsheets associated with G

2020-08-13 03:01发布

问题:

Using Google Sheets API v4, I am looking to get the list of spreadsheets attached to my account. I did much research but have not found any solutions for that.

回答1:

To get list of all files of specific type, you can use drive API v3. Below is an example to get all sheets from drive of authenticated user.

drive.files.list({
    q: "mimeType='application/vnd.google-apps.spreadsheet'",
    fields: 'nextPageToken, files(id, name)'
}, (err, response) => {
    //Your code
})

You can pass file type in MIME type as 'q' parameter. You can get list of drive MIME types here.

source: https://developers.google.com/sheets/api/guides/migration#list_spreadsheets_for_the_authenticated_user



回答2:

The v4 API doesn't offer a way to list spreadsheets. You'll need to use the Drive API. The Migrate from a previous API page has some details on how to do use the Drive API to do it.



回答3:

For that, you have to use Drives API.

from apiclient import discovery

credentials = get_credential_service_account()
service = discovery.build('drive', 'v3', credentials=credentials)
service.drive().list()

For obtaining "credentials", you can use this code.

Google's official doc is here: https://developers.google.com/drive/v3/reference/changes/list



回答4:

Java Implementation :

String url="https://www.googleapis.com/drive/v3/filesq=mimeType='application/vnd.google-apps.spreadsheet'";

public String getSheets(String url) throws UnsupportedEncodingException {

   RestTemplate restTemplate = new RestTemplate();
      HttpHeaders headers = new HttpHeaders();
      headers.add("Authorization", "Bearer " + "Access Token"); 
      HttpEntity entity = new HttpEntity(headers);
      HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
      return response.getBody();
}

Access token can be generated by using below link OAUth Plyaground