function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
manimmanim 

Chatter REST API with java web aplication

Hi,

 

I have a java web application deployed in tomcat,which needs to access chatter data on regular basis.

 

As per the documentation,I need to use chatter REST API to get an access to data.

We need to login first by registering our web application and get an access token ,which is then passed to every GET request to the chatter.

 

I have some doubts regarding this:

 

1.What is the validity of this access token?If I want to fetch data on daily basis do i need to generate it on daily basis?

 

2. Why can't we use existing saleforce login account for accessing chatter REST API?

 

Thanks.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ChrisOctagonChrisOctagon

Hi manim,

 

1) Do you have two different Salesforce organizations? Maybe one is a Developerforce organization and one is a regular corporate Salesforce org? Or one is a developer organization for the Chatter REST API Dev Preview and one is your regular organization?

 

If you are getting the wrong instance URL back then you are probably using the wrong credentials (consumer key, consumer secret, or username/password) when you log in. One thing you might want to try is after you log in, make a request against the "identity URL" that is returned (using the OAuth access token) and see what information it returns about your logged-in user. You may find that they are not what you expected.

 

2) Assuming that you have after all been granted OAuth access to an organization on na10.salesforce.com, the error message you are getting indicates that the Chatter REST API is not enabled for your organization, although the regular REST API is (because the query endpoint is working for you). The Chatter REST API is currently in Dev Preview. If your organization has signed up for the preview, you might want to talk to Customer Support to make sure that it is setup correctly.

All Answers

ChrisOctagonChrisOctagon

Hi manim,

 

The Chatter REST API uses OAuth2 to handle authentication and authorization. OAuth2 allows an end-user to authenticate themselves and authorize that a client application be given access to protected data. Client applications access the Chatter REST API using an access token assigned to them.

 

You can definitely use an existing salesforce login account for accessing the chatter REST API. Take a look at the "web server flow" described here: https://login.salesforce.com/help/doc/en/remoteaccess_oauth_web_server_flow.htm

 

Using this strategy, your web application can redirect an end-user to log into their Salesforce account on a Salesforce web page. Then your web application can retrieve the resulting "authorization code" and exchange it for an "access token" from Salesforce. The access token can then be associated with the user's web session, and you can drop it when the session expires. Note that an access token may also be revoked by an end-user through the "personal information" Setup page on Salesforce.

 

Cheers!

 

 

manimmanim

Thanks for detailed explanation.I went through the material you mentioned.

 

We have a requirement where chatter data needs to be fetched on regular interval and needs to be achieved without any manual intervention.

 

For e.g. I will have one saleforce account dedicated for this functionality which will be used for automatically logging in andfetching the data and displaying on the web page.

 

 

I deployed sample web application given on saleforce and I was able to get  an access token .

I did following to achieve this:

  •  Register web application from setup-->dev--->manage remote application
  • Get consumer key and consumer secret key
  • Update servlet to have this keys as an init parameter

Is there any way this can be automated?

 

 

 

 

ChrisOctagonChrisOctagon

I don't think I entirely understand your question. I do not think that making a new remote application can be automated. You only have to do it once anyways.

 

If you log into the Salesforce account through OAuth you can get an access token and a refresh token back. Then your application can use the access token repeatedly until it expires, when it can automatically use the refresh token to get a new access token. Therefore you only really need to log in once in order to get the initial access and refresh tokens. If you wanted, you could add an "admin setup page" of a sort to your application such that an admin could come in and do the OAuth login once, to get the initial tokens, which could then be stored in a database or config file for use from the main application.

manimmanim

Thanks for reply.

 

I will try making my requirement clear.

I want to show chatter data on home page of my web application.This data will be fetched by daemon thread which will automatically login and pull data out of chatter using REST api.



I have some doubts here

1.Is remote web application registration needs to be done only once?

2.What is the validity of access token? If access token needs to be generated only once I will hard code it in my code.

As per your explaination token can be refreshed after expiration.Is this a manual process or there is any API exposed?

 

 

 

 

 

 

 

 

 

 

ChrisOctagonChrisOctagon

I think the strategy I described should work for you. The Salesforce user logs in once, then when your application makes the successful exchange of the authorization "code" for the access and refresh tokens, your application could automatically store the access/refresh tokens in a place that your daemon can access, then it could tell the daemon to start.


Remote web application registration only needs to be done once for your organization. Registering a web application establishes metadata such as the consumer key, consumer secret, and callback URL. If your applications are doing OAuth in a safe way (such as making sure that the secret is only used by trusted software on trusted systems, and is never stored some place unsafe like an end-user's phone) then you should have no need to register a new application.


The access token is an opaque value used by an authorized application to identify itself to the Chatter REST API. It is safer to use an access token than to hardcode the login credentials of a user into your application. This is because the access token may be revoked at any time by the user through their Salesforce Setup page if they suspect foul play.


Access tokens have a limited lifetime. Your daemon can request a new access token using the refresh token in an HTTP request as described here: https://login.salesforce.com/help/doc/en/remoteaccess_oauth_refresh_token_flow.htm No user interaction is required.

manimmanim

Thanks very much .

I will make the changes in my web application as per your suggestion and let you know how it works .

Thanks again.

 

ChrisOctagonChrisOctagon

Alternatively, since this is a pure server-to-server integration use case, if your server is secure, you could just hardcode the username and password into your application and use the simpler username and password flow: https://login.salesforce.com/help/doc/en/remoteaccess_oauth_username_password_flow.htm Generally this flow is not advised, but in a server-to-server situation it may be an option.

 

manimmanim

Thanks for your help.

Web server flow seems to be working for my web application.

I was able to see my profile by using URI as  "services/data/v22.0/chatter/users/me" and applicattion instance "https://ap1.salesforce.com".

 

Is this instance of SF live one? If not ,what do I need to do to get an access to live data?

Can I retrieve posts and comments using this approach?

 

 

ChrisOctagonChrisOctagon

This instance is live. The "ap1" refers to the Asia Pacific instance (as listed here: http://trust.salesforce.com/trust/status/ ).

 

You can retrieve posts and comments. Please refer to the developer's guide located here: http://www.salesforce.com/us/developer/docs/chatterapipre/salesforce_chatter_rest_api.pdf 

 

For example, you could get your newsfeed using a URI ending in: /chatter/feeds/news/me/feed-items The response is a list of feed items, along with comment pages that can be used to iterate over comments.

manimmanim

Thanks a lot for your inputs.I wnet thorugh this PDF.

This PDF explains about URI's which are used to fetch resources either for me or for specified user id.

I wanted all the posts along with their comments and likes for all the users .

Is this possible?

 

 

 

 

 

ChrisOctagonChrisOctagon

There is no direct way of getting "everything" in a single feed through the Chatter REST API.

 

You can only access the posts/comments/likes that the user you have logged on with has access to, although I guess you could use a special user that has access to everything. However, if you just try to brute-force collect all the items from all the group/user feeds, you would start getting errors because you would run into rate limits.

 

Instead of using the Chatter REST API, I believe that you could instead implement this sort of support for posts and comments by registering an Apex trigger to execute when a new post or comment is made. However I don't think there is support for "like" triggers in Apex.

manimmanim

Thanks for detailed explanation.

I have some newbie questions in mind.

 

1 ) . I already have saleforce account which I access using URL of the form "na10.salesforce.com".

When I login using this URL through browser I get all the groups I belong to and all the posts which are there and many other details.

 

When I login using instance URL "ap1.salesforce.com" , I am not able to see the data for e.g. my group details and posts which are there in na10 instance.

 

I think ap1 instance is only for development purpose and does not contain any live data.Please confirm.

When I will deploy my app on production I will need live data which is there in na10 instance.

 

2 )

 

When I tried to fetch data using following URL in get method:

"https://na10.salesforce.com/services/data/v22.0/chatter/users/me/followers"

 

I got an error as below:

[{"message":"The Chatter Connect API is not enabled for this organization or user type.","errorCode":"API_DISABLED_FOR_ORG"}]

 

What needs to be done for this?

 

When I used URL like below:

String url = "https://na10.salesforce.com/services/data/v22.0/query" and passed a query it worked fine.

 

I am confused here.What is the difference between both of these ways and where can I get sample queries for 2nd approach?

 

Thanks.

 

 

ChrisOctagonChrisOctagon

Hi manim,

 

1) Do you have two different Salesforce organizations? Maybe one is a Developerforce organization and one is a regular corporate Salesforce org? Or one is a developer organization for the Chatter REST API Dev Preview and one is your regular organization?

 

If you are getting the wrong instance URL back then you are probably using the wrong credentials (consumer key, consumer secret, or username/password) when you log in. One thing you might want to try is after you log in, make a request against the "identity URL" that is returned (using the OAuth access token) and see what information it returns about your logged-in user. You may find that they are not what you expected.

 

2) Assuming that you have after all been granted OAuth access to an organization on na10.salesforce.com, the error message you are getting indicates that the Chatter REST API is not enabled for your organization, although the regular REST API is (because the query endpoint is working for you). The Chatter REST API is currently in Dev Preview. If your organization has signed up for the preview, you might want to talk to Customer Support to make sure that it is setup correctly.

This was selected as the best answer
manimmanim

Yes.You are right.I have mailed customer support for getting API enabled.

Thanks a lot for your help.