我不得不向组织内的聊天组发帖 当用户提交他的授权申请时。 我在网上搜索,发现有很多帖子,但没有人谈到向聊天组发表评论所需的所有步骤。 通过这篇文章,我想帮助其他正在寻找类似解决方案的用户。
首先,必须创建一个salesforce开发者帐户。
1)登录salesforce开发者帐户并点击设置 - >构建 - >创建 - >应用 向下滚动并点击已连接的应用程序部分中的新建,然后输入基本信息创建连接的应用程序,然后单击启用OAuth设置以打开API部分
2)获取消费者密钥和秘密。 这是解释此过程的URL (向下滚动到“为您的应用程序配置OAuth 2.0访问”主题)
https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com
3)现在点击登录用户名 - >我的设置 - > Personal->重置我的安全令牌 点击此按钮后,系统会向您的注册电子邮件帐户发送一封电子邮件,其中包含安全令牌。
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)回到聊天室,你应该看到一条消息和abc.pdf。
希望它有所帮助。