0

ユーザーが助成金の申請書を提出したときに、組織内のチャット グループにメッセージを投稿する必要がありました。Web を検索していたところ、多くの投稿があることがわかりましたが、チャット グループにコメントを投稿するために必要なすべての手順について誰も話しませんでした。この投稿を通じて、同様のソリューションを探している他のユーザーを支援したいと思います。

何よりもまず、Salesforce 開発者アカウントを作成する必要があります。

1) Salesforce 開発者アカウントにログインし、[セットアップ] -> [ビルド] -> [作成] -> [アプリ] をクリックします

2) コンシューマ キーとシークレットを取得します。このプロセスについて説明している URL は次のとおりです (トピック「アプリケーションの OAuth 2.0 アクセスの構成」までスクロールします)。

https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com

3) ログインしたユーザー名 -> [マイ設定] -> [個人] -> [マイ セキュリティ トークンのリセット] をクリックすると、セキュリティ トークンが記載された電子メールが登録済みの電子メール アカウントに送信されます。

4) Chatter でグループを作成し、グループ名をクリックして、ブラウザ バー ( https://na1x.salesforce.com/...../GroupProfilePage?g=0F9Fxxxxxxxxxxxxf )からグループ ID (g=0F9FXXX) をコピーします。あなたのファイルとそれを保管してください。

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 とともにメッセージが表示されます。

それが誰かに役立つことを願っています。

4

0 に答える 0