当前位置: 首页 > 知识库问答 >
问题:

无法使用Spring RestTem板调用Salesforce API

曾光誉
2023-03-14

我正在开发一个应用程序JavaSalesforce和调用salesforce服务使用Spring RestTemplatAPI,但似乎得到一些错误。有什么帮助吗?

public class RestTemplateExample {

    final static String TOKEN_URL = "https://ap5.salesforce.com/services/oauth2/token";

    public static void main(String[] args) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("client_secret", "XXXXXXXXX");
        headers.add("client_id", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
        headers.add("grant_type", "password");
        headers.add("username", "XXX@XX.com");
        headers.add("password", "XXXXXXXXXXXX");
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

        HttpEntity<String> entity = new HttpEntity<String>("", headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> result = restTemplate.exchange(TOKEN_URL, HttpMethod.POST, entity, String.class);
        System.out.println("TOKEN DETAILS :: "+result.getBody());
    }
}

当我通过邮递员检查时,一切正常。

2017-10-14 18:40:35 DEBUG o.s.web.client.RestTemplate - Created POST request for "https://ap5.salesforce.com/services/oauth2/token"
2017-10-14 18:40:35 DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, */*]
2017-10-14 18:40:35 DEBUG o.s.web.client.RestTemplate - Writing [] using [org.springframework.http.converter.StringHttpMessageConverter@2141a12]
2017-10-14 18:40:37 DEBUG o.s.web.client.RestTemplate - POST request for "https://ap5.salesforce.com/services/oauth2/token" resulted in 400 (Bad Request); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
    at com.example.RestTemplateExample.main(RestTemplateExample.java:29)

https://ap5.salesforce.com/services/oauth2/token?client_id=XXXXXXXX

共有2个答案

施誉
2023-03-14

您正在尝试将查询字符串参数作为标题发送(不同)。以下代码将等同于您在Postman中的测试

public class RestTemplateExample {

    final static String TOKEN_URL = "https://ap5.salesforce.com/services/oauth2/token";

    public static void main(String[] args) {

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(TOKEN_URL)
        .queryParam("client_secret", "XXXXXXXXX")
        .queryParam("client_id", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
        .queryParam("grant_type", "password")
        .queryParam("username", "XXX@XX.com")
        .queryParam("password", "XXXXXXXXXXXX");

        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

        HttpEntity<String> entity = new HttpEntity<String>("", headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> result = restTemplate.exchange(
            builder.build().encode().toUri(), 
            HttpMethod.POST, 
            entity, String.class
        );
        System.out.println("TOKEN DETAILS :: "+result.getBody());
    }
}
聂建茗
2023-03-14

这需要像下面一样实施,效果很好。

// create the Map of the MultiValueMap
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("grant_type", "password");
map.add("client_id", "XX");
map.add("client_secret", "XXX");
map.add("username", "XX");
map.add("password", "XXX");

// RestTemplate
RestTemplate restTemplate = new RestTemplate();
Map<String, String> token = restTemplate.postForObject(TOKEN_URL, map, Map.class);
System.out.println("--------------------------------------------");
System.out.println("Access Token  :: "+token.get("access_token"));
System.out.println("Instance Url  :: "+token.get("instance_url"));
System.out.println("Id  :: "+token.get("id"));
System.out.println("Token_Type  :: "+token.get("token_type"));
System.out.println("Signature  :: "+token.get("signature"));
System.out.println("--------------------------------------------");

令牌网址将是下面。

final static String TOKEN_URL = "https://login.salesforce.com/services/oauth2/token";

以下是供参考的输出:

--------------------------------------------
Access Token  :: 00D7F0000001I8v!ARgAQB7R8VtkU8QbUYHfdBjytHPATIDYmMKkPt7sunvUW4DeM2t.c.wFDJ0.k0uNPogX5aPWIrL_lHkrbfNai_t.byjjRCy_
Instance Url  :: https://ap5.salesforce.com
Id  :: https://login.salesforce.com/id/00D7F0000001I8vUAE/0057F000000l2bgQAA
Token_Type  :: Bearer
Signature  :: veYhITIlhqf9SJqvBAZ4PKiLh1sj9at13D3m44TnBxw=
--------------------------------------------
 类似资料:
  • 我试图在点击事件上使用ajax调用servlet。从那个servlet,我称之为谷歌身份验证endpoint。我尝试将标头设置为我正在调用的servlet,但我无法摆脱此错误 XMLHttpRequest 无法加载 https://accounts.google.com/o/oauth2/auth?client_id=2536-a...nid 个人资料电子邮件 这是代码 在servlet上,我将其

  • 问题内容: 如何在不返回值的模板中执行功能?这是示例: 在play.golang.org中看到此代码 我知道模板中的逻辑不应该太多,但是对无返回值的运行函数的无知对我来说似乎是一个有趣的问题。 问题答案: Go中的模板与其他语言(例如PHP)中的模板不同。使用为您的模板创建自定义功能。 或者,尝试使用游乐场版本。

  • 错误: /usr/bin/python3.5 /root/PycharmProjects/Capstone2/main.pyTraceback(最近调用的最后一次):File"/root/PycharmProjects/Capstone2/main.py",第62行,在canvas.show()File"/usr/lib/python3/dist-pack/matplotlib/backend/b

  • 问题内容: 假设我有 如何从html / template使用此方法?我的模板中需要这样的东西: 问题答案: 只需省略括号就可以了。例: 根据文档,您可以调用任何返回一个值(任何类型)或两个值(如果第二个是type)的方法。在后一种情况下,如果该错误为非nil ,则将返回该错误并停止执行模板。

  • 我有一个问题,也许有人可以帮助我。我正在尝试构建一个简单的java aws lambda并使用无服务器框架部署它。 我试图建立一个简单的lambda使用: 当我尝试使用调用远程函数时 它起作用了。 但是,当我尝试在本地调用它时,出现以下错误: 编辑:要在本地运行它,我使用: 我在谷歌上搜索了一下,什么也没找到。我不理解这个错误。看起来像是关于maven的?

  • 在向kafka写入主题时,出现错误:: 当使用命令创建一个新主题时,它是确定的。 这是使用Java的生产者代码: 哪里错了