简体   繁体   English

将 Salesforce 数据公开为 Rest API

[英]Exposing the Salesforce Data as an Rest API

I am new to Salesforce and I would like to get some expert advice on how I can expose the Sales force data as an Rest API so the external System can consume it.我是 Salesforce 的新手,我想就如何将 Sales force 数据公开为 Rest API 以便外部系统可以使用它获得一些专家建议。 I was think if I can create a Apex Class like below我想如果我可以创建一个像下面这样的 Apex 类

@RestResource(urlMapping='/GetAccounts/*')
global with sharing class GetAccounts {
    @HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account result = [SELECT Id, Name FROM Account WHERE Id = :accountId];
        return result;
    }
}

And for the external user to consume the data I thought I can set up up a Connected App and provide them with the Username,password, Consumer Key,Consumer secret and they should be authenticating in to Salesforce to get the URI and session ID.对于使用数据的外部用户,我想我可以设置一个连接的应用程序并为他们提供用户名、密码、消费者密钥、消费者秘密,他们应该在 Salesforce 中进行身份验证以获取 URI 和会话 ID。 Using the SessionID and URI the should be able to call the API that is exposed above.使用 SessionID 和 URI 应该能够调用上面公开的 API。 Is this the right approach, please let me know if I am missing anything here.这是正确的方法吗,如果我在这里遗漏了什么,请告诉我。

Also there is a requirement to use Swagger with the API, is it possible to use the Swagger within the Apex Class.还需要将 Swagger 与 API 一起使用,是否可以在 Apex 类中使用 Swagger。 Can you please help how I can leverage Swagger with my API here.你能帮助我如何在我的 API 中利用 Swagger。

First of all you should try to use Salesforce standard REST API.首先,您应该尝试使用 Salesforce 标准 REST API。 You can check the full documentation from here.您可以从此处查看完整文档。 https://developer.salesforce.com/docs/api-explorer/sobject/Account https://developer.salesforce.com/docs/api-explorer/sobject/Account

You might be asking yourself, well when I should expose an APEX class as a REST API like the code you have provided?您可能会问自己,什么时候我应该像您提供的代码那样将 APEX 类公开为 REST API? You need to do that when you need custom logic to be performed and combined with the API call.当您需要执行自定义逻辑并将其与 API 调用结合时,您需要这样做。

Exposing Salesforce REST API as OPEN API specification(Swagger) is not yet supported.尚不支持将 Salesforce REST API 作为 OPEN API 规范(Swagger)公开。 You can vote for this idea if you need it.如果您需要,您可以为这个想法投票。 https://success.salesforce.com/ideaView?id=0873A000000cQsxQAE https://success.salesforce.com/ideaView?id=0873A000000cQsxQAE

But the other way is supported.但支持另一种方式。 You can import a swagger specification file and invoke it using point and clicks from Salesforce.您可以导入 swagger 规范文件并使用来自 Salesforce 的点击来调用它。 Check this blog for more details: https://andyinthecloud.com/2017/07/23/simplified-api-integrations-with-external-services/查看此博客了解更多详情: https : //andyinthecloud.com/2017/07/23/simplified-api-integrations-with-external-services/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM