Create Entry in Activity Stream from XPages via the Social SDK and OAuth2

Create Entry in Activity Stream from XPages via the Social SDK and OAuth2
====================================================================================================================

http://www.slideshare.net/niklasheidloff/ibm-connect-ad206-ibm-domino-xpages-embrace-extend-integrate slides 24 - 27



1) Create OAuth Tokens
====================================================================================================================

cd /opt/IBMConnections/WebSphere/AppServer/profiles/Dmgr01/bin
./wsadmin.sh -user admin -password passw0rd -port 8889 -lang jython 
execfile(‘oauthAdmin.py’)

OAuthApplicationRegistrationService.addApplication(“playground”, “playground”, 
"https://qs.renovations.com/SBTPlayground.nsf/xsp/.sbtservice/oauth20_cb")

clientSecret = OAuthApplicationRegisrationService.getApplicationById(“qsSample”).get(‘client_secret’)

print clientSecret


2) faces-config.xml
====================================================================================================================

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
  <managed-bean>
    <managed-bean-name>connections</managed-bean-name>
    <managed-bean-class>
			com.ibm.sbt.services.endpoints.ConnectionsOAuth2Endpoint
		</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
      <property-name>url</property-name>
      <value>https://qs.renovations.com:444</value>
    </managed-property>
    <managed-property>
      <property-name>serviceName</property-name>
      <value>SBTK</value>
    </managed-property>
    <managed-property>
      <property-name>appId</property-name>
      <value>playground</value>
    </managed-property>
    <managed-property>
      <property-name>consumerKey</property-name>
      <value>playground</value>
    </managed-property>
    <managed-property>
      <property-name>consumerSecret</property-name>
      <value>XKFm2qpQCw4UlYaahkdoNgGBR8hODlexwTCW42KLHehwzAYNbgOJHbsoyIZ7</value>
    </managed-property>
    <managed-property>
      <property-name>authorizationURL</property-name>
      <value>https://qs.renovations.com:444/oauth2/endpoint/connectionsProvider/authorize</value>
    </managed-property>
    <managed-property>
      <property-name>accessTokenURL</property-name>
      <value>https://qs.renovations.com:444/oauth2/endpoint/connectionsProvider/token</value>
    </managed-property>
    <managed-property>
      <property-name>forceTrustSSLCertificate
			</property-name>
      <value>true</value>
    </managed-property>
  </managed-bean> 
  <!--AUTOGEN-START-BUILDER: Automatically generated by IBM Domino Designer. Do not modify.-->
  <!--AUTOGEN-END-BUILDER: End of automatically generated section-->
</faces-config>


3) Java Code
====================================================================================================================

package org.openntf.connections.activitystream.samples;

import com.ibm.sbt.services.client.activitystreams.ActivityStreamService;
import com.ibm.sbt.services.client.connections.communities.CommunityService;
import com.ibm.commons.util.io.json.JsonJavaFactory;
import com.ibm.commons.util.io.json.JsonJavaObject;
import com.ibm.commons.util.io.json.JsonParser;
import java.util.UUID;
import java.util.HashMap;

public class AddToActivityStream {

	static public void createEntryInCommunityActivityStreamURL(String nId, String text) throws Exception {					
		JsonJavaObject jsonObject = getJSONForActivityStreamEntry(
				"/homepage/nav/common/images/iconProfiles16.png",
				"Generator ID",
				"Generator display name",
				"http://xpages.info",
				"@me",
				"Collaboration Today News",
				"I've posted a new entry on CollaborationToday.info.",
				text,
				"note",
				UUID.randomUUID().toString(),
				"Object display name", 
				null,
				null, 
				"http://qs.renovations.com/ctqs2.nsf/entry.xsp?nid=" + nId,
				null);

		ActivityStreamService service = new ActivityStreamService();

		HashMap<String, String> headers = new HashMap<String, String>();
	    	headers.put("Content-Type", "application/json");
	    
	    	String communityId = "f923c5f4-12c9-4b0f-9e1c-3c7bb5bf7916"; // XPages community
	    
		service.postEntry("urn:lsid:lconn.ibm.com:communities.community:" + communityId, "@all", "@all", 
				jsonObject, headers);
		
		String debug = "";
	}
	
	
	static public void createEntryInUserActivityStreamURL() throws Exception {					
		JsonJavaObject jsonObject = getJSONForActivityStreamEntry(
				"/homepage/nav/common/images/iconProfiles16.png",
				"Generator ID",
				"Generator display name",
				"http://xpages.info",
				"@me",
				"Collaboration Today News",
				"I've posted a new entry on CollaborationToday.info.",
				"Collaboration Today News",
				"note",
				UUID.randomUUID().toString(),
				"Object display name", 
				null,
				null, 
				"http://collaborationtoday.info",
				"66D5E6C6-9C44-A694-4825-7A700025768E"); // user id from Anna Bauer

		ActivityStreamService service = new ActivityStreamService();

		HashMap<String, String> headers = new HashMap<String, String>();
	    	headers.put("Content-Type", "application/json");	   
	    
		service.postEntry(jsonObject, headers);
	}

	static public JsonJavaObject getJSONForActivityStreamEntry(
			String generatorImage, String generatorId, String generatorDisplayName,
			String generatorUrl, String actorId, String title, String content, String objectSummary,
			String objectType, String objectId, String objectDisplayName, String embedGadget,
			String embedContext, String embedUrl, String deliverTo) throws Exception {
		
		StringBuilder sb = new java.lang.StringBuilder();
		sb.append("{\"generator\": {");
		sb.append("\"image\": { \"url\": \"" + generatorImage + "\"},");
		sb.append("\"id\": \"" + generatorId + "\",");
		sb.append("\"displayName\": \"" + generatorDisplayName + "\",");
		sb.append("\"url\": \"" + generatorUrl + "\""); sb.append("},");
		sb.append("\"actor\": {    \"id\": \"" + actorId + "\"  },");
		sb.append("\"verb\": \"post\",");
		sb.append("\"title\": \"" + title + "\",");
		sb.append("\"content\": \"" + content + "\",");
		sb.append("\"updated\": \"" + new java.util.Date().toString()+ "\",");
		sb.append("\"object\": {");
		sb.append("\"summary\": \"" + objectSummary + "\",");
		sb.append("\"objectType\": \"" + objectType + "\",");
		sb.append("\"id\": \"" + objectId + "\",");
		sb.append("\"displayName\": \"" + objectDisplayName + "\""); sb.append("},");
		sb.append("\"openSocial\":{"); sb.append("\"embed\": {");
		if (embedGadget != null) {
			sb.append("\"gadget\": \"" + embedGadget + "\",");
			sb.append("\"context\": " + embedContext + "}");
		}
		else {
			sb.append("\"url\": \"" + embedUrl + "\"}");
		}		
		if (deliverTo != null) {		
			sb.append(",\"deliverTo\" : [{ \"objectType\" : \"person\", \"id\" : \"" + deliverTo + "\"}]");		
		}
		sb.append("}}");		               
		String input = sb.toString();
		JsonJavaFactory factory = JsonJavaFactory.instanceEx;
		JsonJavaObject jsonObject = (JsonJavaObject) JsonParser.fromJson(factory, input);

		return jsonObject;
	}
}





Create Entry in Activity Stream in IBM Conenctions from XPages via the IBM Social Business Toolkit SDK and OAuth2

see http://www.slideshare.net/niklasheidloff/ibm-connect-ad206-ibm-domino-xpages-embrace-extend-integrate slides 24 - 27 for details

Java
Niklas Heidloff
February 14, 2013 4:00 AM
Rating
277

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...