Salesforce Apex Google Drive API 正在使用 mimeType“application/json"创建新文件 [英] Salesforce Apex Google Drive API is creating new file with mimeType "application/json"

查看:26
本文介绍了Salesforce Apex Google Drive API 正在使用 mimeType“application/json"创建新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to create a function in apex that will create Google Drive folders when creating a new record in Salesforce.

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

I have managed to authenticate and handle GET requests just fine. My issue is regarding the POST requests. The code below should create a folder with the label provided in the "title" parameter. The script executes and instead creates an extension-less file without a label.

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

我感觉这是我的请求正文,但我不知道如何解决它.

I have a feeling it's my request body but I have no idea what to do to fix it.

推荐答案

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

You've used the wrong endpoint. Instead of the file endpoint, you're posting to the content endpoint.

所以

req.setEndpoint('https://www.googleapis.com/upload/驱动器/v2/文件');

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

应该是(对于 v2 API)

should be (for the v2 API)

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

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

或(对于 v3 API)

or (for the v3 API)

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

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

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

If you use v3 (which you probably should), your json should change thus

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

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

这篇关于Salesforce Apex Google Drive API 正在使用 mimeType“application/json"创建新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆