使用java向salesforce chatter发布评论和附件
我不得不给我们组织内的一个聊天小组发一条信息 当用户提交其授权申请时。 我在网上搜索,发现有很多帖子,但没有人谈论将评论发布到ChatterGroup所需的所有步骤。 通过这篇文章,我想帮助其他正在寻找类似解决方案的用户 首先,必须创建一个salesforce开发人员帐户 1登录salesforce开发者帐户,然后单击设置->构建->创建->应用程序 向下滚动并单击“已连接的应用程序”部分中的“新建”,通过输入基本信息创建已连接的应用程序,然后单击“启用OAuth设置”打开API部分 2获取使用者密钥和密码。 下面是解释此过程的URL 向下滚动至主题“为应用程序配置OAuth 2.0访问” 3现在点击登录用户名->我的设置->个人->重置我的安全令牌 单击此按钮后,将向您注册的电子邮件帐户发送一封带有安全令牌的电子邮件 4在Chatter中创建一个组并单击组名,然后将组idg=0F9FXXX从浏览器栏复制到您的文件中并保留它 5完成chatter之后,让我们转到Java应用程序,将评论发布到一个组 下面是我用来发布注释和附件的java助手类。 commons-httpclient-3.1和httpclient-4.3.jar都是jsonxxxx.jar所必需的使用java向salesforce chatter发布评论和附件,salesforce,salesforce-chatter,Salesforce,Salesforce Chatter,我不得不给我们组织内的一个聊天小组发一条信息 当用户提交其授权申请时。 我在网上搜索,发现有很多帖子,但没有人谈论将评论发布到ChatterGroup所需的所有步骤。 通过这篇文章,我想帮助其他正在寻找类似解决方案的用户 首先,必须创建一个salesforce开发人员帐户 1登录salesforce开发者帐户,然后单击设置->构建->创建->应用程序 向下滚动并单击“已连接的应用程序”部分中的“新建”,通过输入基本信息创建已连接的应用程序,然后单击“启用OAuth设置”打开API部分 2获取使用
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
希望能对某些人有所帮助。您可能希望将此问题转移到专门的销售人员听众那里。此外,问题和答案应该分开。回答你自己的问题没关系,但应该在回答部分。好的,谢谢,我将提出这个问题。我应该在这里删除它吗?