Get the list of all spreadsheets associated with Google account using Sheets API v4

跟風遠走 提交于 2020-01-03 08:07:48

问题


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:


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.




回答2:


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




回答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



来源:https://stackoverflow.com/questions/37876423/get-the-list-of-all-spreadsheets-associated-with-google-account-using-sheets-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!