当用户提交他的资助申请时,我不得不向我们组织内的聊天组发布消息。我在网上搜索,发现有很多帖子,但没有人谈论在聊天组发表评论所需的所有步骤。通过这篇文章,我想帮助一些正在寻找类似解决方案的其他用户。
首先,必须创建一个销售人员开发人员帐户。
1) 登录 Salesforce 开发人员帐户并单击设置 -> 构建 -> 创建 -> 应用程序向下滚动并单击已连接应用程序部分中的新建,并通过输入基本信息创建连接的应用程序,然后单击启用 OAuth 设置以打开 API 部分
2) 获取消费者密钥和秘密。这是解释此过程的 URL(向下滚动到主题“为您的应用程序配置 OAuth 2.0 访问”)
https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com
3) 现在点击登录的用户名-> 我的设置-> 个人-> 重置我的安全令牌一旦你点击这个,一封带有安全令牌的电子邮件将被发送到您注册的电子邮件帐户。
4) 在 Chatter 中创建一个群组,然后单击群组名称并将群组 ID(g=0F9FXXX) 从浏览器栏 ( https://na1x.salesforce.com/...../GroupProfilePage?g=0F9Fxxxxxxxxxxxxxf ) 复制到你的文件并保留它。
5) 完成聊天后,让我们转到 Java 应用程序向群组发表评论
这是我用来发表评论和附件的 java 助手类。需要 commons-httpclient-3.1 和 httpclient-4.3.jar 以及 jsonxxxx.jar
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
public class ChatterHelper
{
public static void postOnChatterGroup()
{
public static final String SALESFORCE_USERNAME = "xyz@abc.com";
public static final String SALESFORCE_PASSWORD = "password+securityToken";
public static final String SALESFORCE_CONSUMER_KEY="3MVG9JZ_r.Qzxxx.xxx.xxxx.xxx.xx1";
public static final String SALESFORCE_SECRET = "345345345345345345";
public static final String SALESFORCE_CHATTER_GROUP = "0F9xxxxxxxxxxx";
try
{
StringBuffer sbf = new StringBuffer();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).
setConnectTimeout(30000).build();
CloseableHttpClient httpClient = HttpClients.createDefault();
String baseUrl = "https://na1.salesforce.com/services/oauth2/token";
// Send a post request to the OAuth URL.
HttpPost oauthPost = new HttpPost(baseUrl);
oauthPost.setConfig(requestConfig);
List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>();
// keep this as it is
parametersBody.add(new BasicNameValuePair("grant_type", "password"));
parametersBody.add(new BasicNameValuePair("username", SALESFORCE_USERNAME));
parametersBody.add(new BasicNameValuePair("password", SALESFORCE_PASSWORD));
parametersBody.add(new BasicNameValuePair("client_id", SALESFORCE_CONSUMER_KEY));
parametersBody.add(new BasicNameValuePair("client_secret", SALESFORCE_SECRET));
oauthPost.setEntity(new UrlEncodedFormEntity(parametersBody));
// Execute the request.
HttpResponse response = httpClient.execute(oauthPost);
HttpEntity entity = response.getEntity();
if (entity != null)
{
BufferedReader rd = new BufferedReader(new InputStreamReader(
entity.getContent(), "UTF-8"));
String line = "";
while ((line = rd.readLine()) != null)
{
sbf.append(line);
}
}
JSONObject jObj = new JSONObject(sbf.toString());
String accessToken = jObj.get("access_token").toString();
String instanceUrl = jObj.get("instance_url").toString();
String textMsg = "Here is the comment";
File contentFile = new File("c:/xyz/abc.pdf");
String desc = "This file is uploaded by xyz user";
String fileName = "View PDF";
final PostMethod postMethod = new PostMethod(instanceUrl +
"/services/data/v23.0/chatter/feeds/record/" +
SALESFORCE_CHATTER_GROUP + "/feed-items");
Part[] parts = {
new StringPart("desc", desc),
new StringPart("fileName", fileName),
new StringPart("text", textMsg),
new FilePart("feedItemFileUpload", contentFile),
};
postMethod.setRequestEntity(new MultipartRequestEntity(parts,
postMethod.getParams()));
postMethod.setRequestHeader("Authorization", "OAuth " + accessToken);
postMethod.addRequestHeader("X-PrettyPrint", "1");
HttpClient client = new HttpClient();
client.getParams().setSoTimeout(Constant.URL_SOCKET_TIMEOUT);
client.executeMethod(postMethod);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
6) 回到 chatter,您应该会看到一条消息以及 abc.pdf。
希望它对某人有所帮助。