【问题标题】:Get the list of all spreadsheets associated with Google account using Sheets API v4使用 Sheets API v4 获取与 Google 帐户关联的所有电子表格的列表
【发布时间】:2016-06-17 08:13:34
【问题描述】:

使用 Google Sheets API v4,我希望获取附加到我的帐户的电子表格列表。我做了很多研究,但没有找到任何解决方案。

【问题讨论】:

    标签: java google-sheets google-spreadsheet-api google-sheets-api


    【解决方案1】:

    v4 API 不提供列出电子表格的方法。您需要使用 Drive API。 Migrate from a previous API page 有一些关于如何使用 Drive API 来做的细节。

    【讨论】:

    • 我已经搜索了 6 个小时,但是嘿@Sam 但是如何阅读此响应,看起来 XML 格式不正确。我可以得到 JSON 响应吗
    【解决方案2】:

    要获取特定类型的所有文件的列表,您可以使用 drive API v3。 下面是一个从认证用户的驱动器中获取所有工作表的示例。

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

    您可以将 MIME 类型中的文件类型作为 'q' 参数传递。您可以获取驱动器 MIME 类型列表here

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

    【讨论】:

      【解决方案3】:

      为此,您必须使用 Drives API。

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

      获取“凭证”可以使用this code

      谷歌官方文档在这里:https://developers.google.com/drive/v3/reference/changes/list

      【讨论】:

        【解决方案4】:

        Java 实现:

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

        可以使用以下链接生成访问令牌 OAUth Plyaground

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-21
          相关资源
          最近更新 更多