【问题标题】:Salesforce Apex Google Drive API is creating new file with mimeType "application/json"Salesforce Apex Google Drive API 正在使用 mimeType“application/json”创建新文件
【发布时间】:2017-08-25 04:43:15
【问题描述】:

尝试在 apex 中创建一个函数,该函数将在 Salesforce 中创建新记录时创建 Google Drive 文件夹。

我已经成功地验证并处理了 GET 请求。我的问题是关于 POST 请求。下面的代码应该使用“title”参数中提供的标签创建一个文件夹。脚本执行并改为创建一个没有标签的无扩展名文件。

public void getDriveFiles(HttpResponse authResponse) {
    http h = new Http();
    Httprequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();      

    Map<String, Object> responseObject = (Map<String, Object>) JSON.deserializeUntyped(authResponse.getBody());

    req.setEndpoint('https://www.googleapis.com/upload/drive/v2/files');
    req.setMethod('POST');
    req.setHeader('Content-type', 'application/json');
    req.setHeader('Authorization', 'Bearer '+responseObject.get('access_token'));

    String jsonData = '{"title":"NewFolder", "mimeType":"application/vnd.google-apps.folder"}';
    req.setBody(jsonData);
    res = h.send(req);

    system.debug('Response ='+ res.getBody() +' '+ res.getStatusCode());
}

我感觉这是我的请求正文,但我不知道如何修复它。

【问题讨论】:

  • 你能把你的system.debug的输出贴出来
  • @pinoyyid 请参阅上面的编辑以获取调试输出

标签: google-drive-api salesforce apex


【解决方案1】:

您使用了错误的端点。您将发布到 content 端点,而不是 file 端点。

所以

req.setEndpoint('https://www.googleapis.com/upload/drive/v2/files');

应该是(对于 v2 API)

req.setEndpoint('https://www.googleapis.com/drive/v2/files');

或(对于 v3 API)

req.setEndpoint('https://www.googleapis.com/drive/v3/files');

如果你使用 v3(你可能应该这样做),你的 json 应该会改变

String jsonData = '{"name":"NewFolder", "mimeType":"application/vnd.google-apps.folder"}';

【讨论】:

  • 非常感谢。解决了我的问题。
  • 没有问题。在 StackOverflow 上,协议是将答案标记为正确,以便其他有相同问题的人受益。
猜你喜欢
  • 2013-06-03
  • 1970-01-01
  • 2020-09-07
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多