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

问题描述

尝试在apex中创建一个功能,以便在Salesforce中创建新记录时创建Google云端硬盘文件夹.

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.

所以

(对于v2 API)应为

should be (for the v2 API)

或(对于v3 API)

or (for the v3 API)

如果使用v3(您可能应该使用),则json应该会因此更改

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

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

08-18 20:57