Coder Social home page Coder Social logo

javachatterrestapi's Introduction

Java Chatter REST API

Think the Chatter REST API is awesome and powerful, but maybe a bit overwhelming? This native Java implementation aims to keep the implementation simple while keeping all the power at your fingertips.

Build status

Build status

Overview

This Java Chatter REST API is a simple library using POJO's to interact with the Chatter REST API.

Usage

Simply download the java/src and required libraries and incorporate it into your project.

Compiling the source is easy using gradle:

gradle assemble

Or to run the unittests as well

gradle build

The jar file can be found at build/lib/JavaChatterRESTApi.jar.

Features

  • Creating Chatter messages, including text, links, @mentions and #tags

  • Posting your status (on your own wall)

  • Posting to somebody else's wall

  • Posting on a group

  • Replying to a thread

  • Searching for users

  • Executing "raw" SOQL queries in case you need some advanced usage*

  • The SOQL queries are executed through the REST API

Configuration and authentication

The Java Chatter REST API supports various forms of authentication. All of them require a form of the IChatterData information to be fed into them, but different methods require different parts of this object to be filled in.

ClientSecret authentication

Also see http://www.salesforce.com/us/developer/docs/api_streaming/Content/code_sample_auth_oauth.htm for how this works.

RefreshToken authentication

If you already possess a Refresh token (from a previous client-secret authentication perhaps) you can use this authentication method.

Username and password

If your org allows it (grant_type=password), you can use the client ID, client Secret and your username and password to authenticate. This is discouraged and the ClientSecret authentication is preferred over this one.

Interactive authentication

This requests information from the user to be returned via the console at runtime. Useful for token-less environments. It does requires a minimum of a Client Key and Client Callback to be configured.

Example of a configuration file/class to be used with Password authentication

public class ChatterData implements IChatterData {

    private final String apiVersion = "24.0";
    private final String instanceUrl = "https://na10.salesforce.com";

    private final ChatterAuthMethod authMethod = ChatterAuthMethod.PASSWORD;
    private final String username = "[email protected]";
    private final String password = "myPassword";

    private final String clientKey = "3MVG9yZ.WNe6byQDS1oBDJg6vP82qy7w.OVregoIATuJtBxxIxDQmb8kr8zmasqSUAsCED6CCNx.3zaWScqph";
    private final String clientSecret = "6830641966138152974";
    
    // And the required get/set methods
}

Examples

You can find loads of practical examples in the test/integration/src/ folder under the com.salesforce.chatter package. For example: TestChatterPost.java

A very simple example:

ChatterService service = new ChatterService(new ChatterData());

Message msg = new Message();
msg.addSegment(new TextSegment("Hey "));
msg.addSegment(new MentionSegment(JASPER_PROFILE_ID));
msg.addSegment(new TextSegment(", check out this website: "));
msg.addSegment(new LinkSegment("www.salesforce.com"));
msg.addSegment(new TextSegment(" #salesforce"));

File img = new File("img.png");
msg.addAttachment(new ImageAttachment("Title", "Description", "image/png", img));

ChatterCommand cmd = new PostToThreadCommand(TEAM_GROUP_ID);

service.executeCommand(cmd, msg);

This contains almost all information you need. You create a simple Message object which holds whatever it is you would like to post (and which supports text, links and @mentions). The ChatterCommand decides where this message should go (to your own or somebody's wall, a particular group or as a response to somebody's thread). The ChatterData holds all the authentication information while the ChatterService takes care of making the magic happen

License

The BSD 2-Clause License

http://opensource.org/licenses/BSD-2-Clause

See LICENSE.txt

javachatterrestapi's People

Contributors

jasperroel avatar kamilsmuga avatar svc-scm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

javachatterrestapi's Issues

UnrecognizedPropertyException in ClientSecretAuthentication method

When a successful response is received from Chatter via the ClientSecretAuthenication (sic) method, a org.codehaus.jackson.map.exc.UnrecognizedPropertyException is thrown on the id field because the ChatterAuthToken class does have a valid property for the id. Other valid properties are also not defined on the ChatterAuthToken.

Two solutions include:

  1. Add the @JsonIgnoreProperties(ignoreUnknown = true) annotation to the ChatterAuthToken class.
  2. Define the other valid properties (id, issued_at, scope, instance_url, refresh_token, signature).

Seems to me that the second option is better because in order to use the refresh token it is necessary to know what it is (for safe persistence, for example).

ChatterService executeCommand(ChatterCommand, Message) should return HttpMethod

Currently the ChatterService.executeCommand(ChatterCommand, Message) method does not return the HttpMethod and thus provides no way of handling any errors that might occur. Posts can fail silently because the response is ignored.

Simple solution is to return the HttpMethod like the executeCommand(ChatterCommand) method does. This allows for inspection of the response status and body so that errors can be handled appropriately.

I have a fix that I can push if permitted.

ClientSecretAuthentication incorrectly URL encodes client id and callback

The authorize() method of the ClientSecretAuthenication (sic) class URL encodes the client id and the callback which causes redirect_uri_mismatch errors on callback values that contain character that require URL encoding. The reason is that the values are passed in an HTTP POST as the body and therefore do not require URL encoding and are not decoded on the Chatter side. The values should be passed to the HTTP POST without any encoding.

I have a local fix I can push if permitted.

`gradle build` fails

Looks like with the missing clover.license file, the gradle build will fail.
Maybe update the readme.md to add where to get a valid license?

Download https://repo1.maven.org/maven2/com/atlassian/clover/clover/4.1.1/clover-4.1.1.jar
[clover-clean] Clover Version 4.1.1, built on December 07 2015 (build-977)
[clover-clean] Loaded from: /root/.gradle/caches/modules-2/files-2.1/com.atlassian.clover/clover/4.1.1/26f74a1fcaf03d231be3ed89df0b28d6e0df8d84/clover-4.1.1.jar
[clover-clean] No license file found.
:test FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':test'.

    java.lang.RuntimeException: Invalid or missing License.. Please visit http://my.atlassian.com to obtain a valid license.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 41.485 secs
[debug] Running post-Build Cache steps
[info] build failed (exit code 1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.