如何创建SalesForce'用户'使用REST API?

时间:2015-07-12 16:10:03

标签: salesforce

我正在尝试在Java客户端上使用REST API在SFDC中创建用户。 请参阅下面的代码并帮助我理解为什么我收到以下错误消息:

HTTP Status 400 ::
{
errorCode: "NOT_FOUND"
message: "The requested resource does not exist"
}

CODE:

HttpClient httpClient = new HttpClient();
JSONObject user = new JSONObject();
String userId = null;

try{

    user.put("Username", "email@domain.com");
    user.put("Alias", "DemoAPI");
    user.put("ProfileId", "00e90000000fKXB");
    user.put("Email", "email123456@domain.com");
    user.put("EmailEncodingKey", "ISO-8859-1");
    user.put("LastName", "REST API Test");
    user.put("LanguageLocaleKey", "pt_BR");
    user.put("LocaleSidKey", "pt_BR");
    user.put("TimeZoneSidKey", "America/Sao_Paulo");

    PostMethod post = new PostMethod( instanceURL + "/services/data/v29.0/sobjects/User");
    post.setRequestHeader("Authorization", "OAuth " + accessToken);
    post.setRequestEntity(new StringRequestEntity(user.toString(), "application/json", null));

    httpClient.executeMethod(post);

}catch(Exception e){}

1 个答案:

答案 0 :(得分:2)

我使用Chrome Postman插件发布以下内容:

POST request to Salesforce REST API

请注意,资源URL设置为:(我假设您基于ProfileId中的pod identifier在AP1上。)

https://na5.salesforce.com/services/data/v29.0/sobjects/User

我收到了回复:

[
    {
        "message": "Duplicate Username.<br>The username already exists in this or another Salesforce organization. Usernames must be unique across all Salesforce organizations. To resolve, use a different username (it doesn't need to match the user's email address). ",
        "errorCode": "DUPLICATE_USERNAME",
        "fields": [
            "Username"
        ]
    }
]

哪个适用于测试目的。它可能会使用不同的用户名和免费许可证。

请确认您的PostMethod网址设置为:

https://ap1.salesforce.com/services/data/v29.0/sobjects/User

请特别检查instanceURL上没有尾部斜杠。