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
h0llyh0lly 

Connect to Chatter Rest API and retrieve data

As a newbie with Chatter I had real problems figuring out the process to retrieve data so hopefully the following will help any other newbies trying to figure it out. Using the Developer Edition:

 

 

1. You will need an https domain where you will run your app which will make a request to your Chatter App
2. You will need to register a new sub domain within the Setup. I think it was under Domains. So you will have something like mydomain-dev-ed.my.salesforce.com

3. Create a Connected App (Create/Apps)
4. The Start URL will be the sub domain you created
5. Select the checkbox for Enable OAuth Settings
6. The callback url will be a path to your https domain on your server (ie https path/to/results.aspx
7. Put all the selected OAuth Scopes over to the right
8. Save

 

_____________________________________________________________________

_____________________________________________________________________

 

In the left side navigation you will see there is Manage Apps and Create/Apps

You will find info about your Connected App in both those locations.
You will need the Consumer Key, Consumer Secret and Callback Url (or redirect url)

You will now create two .net files which will reside on your server:

 

 

1. default.aspx
2. results.aspx

 

_______________________________________________________________________

 

The default.aspx will make the initial call to salesforce which will return a javascript string.
You will need to extract the relevant content from the string to use, which in turn will open a login page.

Once you have logged in, additional values will be passed back to your app.

 

______________________________________________________________________

______________________________________________________________________

 

In your default.aspx

 

 

 

string clientid = "this is the Consumer Key ";
string redirecturl = "https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=" + clientid + "&redirect_uri=" + redirecturl ;
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
[Inside a try/catch put the following]
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
result = webClient.DownloadString(uri);

string returnedString = result;

[The result is the string returned which you will need to extract the url contained within the string]
At this point you need to create a ClientScript which, when called, will load the login page.

String csname2 = "myscript";
Type cstype = this.GetType();
string jsScript = "<script>window.location.href ='" + Your_Extracted_Url + "'</script>";
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(cstype, csname2, jsScript);

 

 

If you save the default.aspx, compiled your project and uploaded to your server then ran it, you would get the login page.

Something like:

 

https://login.salesforce.com/?ec=302&startURL=%2Fsetup%2Fsecur%2FRemoteAccessAuthorizationPage.apexp%3Fsource%3Dl86Hcy6qEKtWNqzXVErZDlmcRQs0D_HBE55MLVuP3x8t5wXkyHYyrVx2Xb5JoWzyVOJrnTqMMaWy_Sfomv.j1xklSQ67NPiEbdAN9Vn15MXpcuc7Tn4&sdtd=1


Now when you submit the login page, it is effectively going to your callback page you designated earlier, so now you have to add code to this file to retrieve the required data.

Result.aspx

[you can add your own try/catch and conditions]

 

string clientid = "same as previous";
string clientsecret = "your client secret";
string redirecturl = "same as previous";

string uri = "https://login.database.com/services/oauth2/token?grant_type=authorization_code&client_id=" + clientid + "&client_secret=" + clientsecret + "&redirect_uri=" + redirecturl + "&code=" + res + "&format=json";


var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";


if (webRequest.HaveResponse && response != null)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
        string result = reader.ReadToEnd();
        JavaScriptSerializer ser = new JavaScriptSerializer();
        Dictionary<string,object> dict = ser.Deserialize<Dictionary<string, object>>(result);
        int dCount = 0;


       foreach (string strVal in dict.Values)
       {
         if (dCount == 6)
             {
                    string chatterApiUri = "https://SubdomainYouCreated.my.salesforce.com/services/data/v23.0/chatter/users/me";
                   var webRequestlogin = (HttpWebRequest)WebRequest.Create(chatterApiUri);
                  webRequestlogin.Headers.Add("Authorization", "OAuth "+ sToken);
                 webRequestlogin.Method = "GET";

                    using (var aresponse = webRequestlogin.GetResponse() as HttpWebResponse)
                    {
                                if (webRequestlogin.HaveResponse && response != null)
                               {
                                        using (var areader = new StreamReader(aresponse.GetResponseStream()))
                                       {
                                              string aresult = areader.ReadToEnd();
                                              lblresult.Text += "HERE IS YOUR RESULT " + aresult;
                                       }
                               }
                   }
 
       }
    }
  }
}

 

 

 

 

Your page on your server should display something like the following:

{"address":{"state":null,"country":"GB","street":null,"city":null,"zip":"EH3 5RT"},"email".......etc

 

 

It's a start but hopefully this will help because the documentation is shoddy.

Wade WegnerWade Wegner
Can you try the recently released .NET Toolkit for Chatter? See details here: http://blogs.developerforce.com/developer-relations/2014/01/announcing-the-salesforce-toolkits-for-net.html