Google Drive REST API的问题

时间:2016-08-09 14:24:36

标签: rest authentication google-drive-api salesforce apex

我正在构建Google和Salesforce之间的集成,为此我是APEX。由于流程和Salesforce的工作方式,我必须创建一个服务帐户并使用它登录。

我现在有一个问题;我登录:一切都很顺利,然后再制作第二个标注尝试获取我的驱动器中最近创建的2条记录:我只收到1个文件 - 带有欢迎文本的PDF和有关如何使用驱动器的信息。

所以我做了一些测试,发现了以下内容:

  1. 如果我使用this并使用OAuth 2.0进行身份验证,我就不会遇到任何问题,而且我能够获得所有想要获取的文档。
  2. 如果我获得了一个访问令牌并将其附加到此URL:https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=xxxx我收到回复,告诉我我的令牌是正确的,并且我需要正确的范围(见下文)
    
    
    {
     "issued_to": "1234567890",
     "audience": "1234567890",
     "scope": "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.metadata https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/drive.photos.readonly https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/drive.scripts",
     "expires_in": 3493,
     "access_type": "offline"
    }
    
    
    
  3. 有没有人对我做错了什么有任何想法?我使用了一个服务帐户来启动我已激活的初始连接"启用Google Apps域范围的委派",我还设置了尽可能多的权限,使用户能够基本上查看和修改所有内容

    -------- --------更新

    代码段

    构建标注的方法:

    public void getRecentImages(){
        Integration_Settings__c settings = Integration_Settings__c.getValues('Google APIs');
    
        String endpoint = settings.Endpoint_URL__c + '/drive/v3/files' +
            '?corpus=user' +
            '&orderBy=createdTime' +
            '&pageSize=2';
    
        Map<String, String> headers = new Map<String, String>();
        headers.put('Authorization', 'Bearer ' + accessToken);
        headers.put('Content-Type', 'application/json');
        headers.put('Charset', 'UTF-8');
    
        String method = 'GET';
    
        HttpResponse res = doCallout(endpoint, method, null, headers);
    
        System.debug(res.getBody());
    
        JSONParser parser = JSON.createParser(res.getBody());
    
        // New instance of GoogleFiles
        Google.GoogleFiles response;
    
        // Try/Catch
        try {
            // Parse JSON into response
            response = (Google.GoogleFiles)parser.readValueAs(Google.GoogleFiles.class);
    
            System.debug(response);
        } catch(Exception e){
            System.debug(e.getLineNumber() + ' - ' + e.getMessage());
        }
    }
    

    进行标注的方法:

    public HttpResponse doCallout(String endpoint, String method, String body, Map<String, String> headers){
        // Instataite new instance of Http
        Http h = new Http();
    
        // Instatiate new instace of HttpRequest
        HttpRequest req = new HttpRequest();
    
        // Set Endpoint URL
        req.setEndpoint(endpoint);
    
        // Set Method Type
        req.setMethod(method);
    
        // Set Body
        if(body != null)
            req.setBody(body);
    
        // If there are headers set Headers
        if(headers != null && headers.size() > 0)
            for(String s : headers.keySet())
                req.setHeader(s, headers.get(s));
    
        // Return HttpResponse
        return h.send(req);
    }
    

    --------更新2 --------

    Salesforce和PostMan标注中返回的JSON:

    {
      "kind": "drive#fileList",
      "files": [
        {
          "kind": "drive#file",
          "id": "0B_07cXloqOyRc3RhcnRlcl9maWxl",
          "name": "Getting started",
          "mimeType": "application/pdf"
        }
      ]
    }
    

    https://developers.google.com/drive/v3/reference/files/list返回的JSON:

    {
      "kind": "drive#fileList",
      "nextPageToken": "xxxxxx",
      "files": [
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.google-apps.folder"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.google-apps.spreadsheet"
        },
        {
          "kind": "drive#file",
          "id": "0B34hDNHXoLmVTFZ1VXhzbHRkUmc",
          "name": "Osney Media - 00038156",
          "mimeType": "application/vnd.google-apps.folder"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.google-apps.folder"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.google-apps.folder"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.google-apps.folder"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.google-apps.folder"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.google-apps.folder"
        },
        {
          "kind": "drive#file",
          "id": "xxxxxx",
          "name": "xxxxxx",
          "mimeType": "application/vnd.google-apps.folder"
        },
        ETC......
      ]
    }
    

2 个答案:

答案 0 :(得分:0)

为了获得2个最近创建的文档,您需要使用&amp; orderBy = createdTime%20desc

如果没有%20desc ,您将获得前2个文档。

顺便说一句,您可能希望在&amp; q = .... 中添加其他搜索参数,以过滤其他不需要的文件,例如:仅列出在某个文件夹下创建或由某些用户拥有的文件夹,并且可能还包括已删除%3Dfalse

答案 1 :(得分:0)

修复方法是更新相关文件夹的共享规则,以便与服务帐户共享内容......这是一种愚蠢的方式,因为它应该具有组织范围......但这就是生命。