How to send a message to the Jive apps gateway from a .Net application

Version 1

    I recently wanted to push notifications from my Apps backend running on Microsoft Azure into Jive, using the Jive apps gateway.

     

    .Net and Oauth are a somewhat difficult combination sometimes. You have either extremely powerful libraries such as dotNetOpenAuth (http://www.dotnetopenauth.net/ ) that do amazing things, but are hard to grok due to their broad capabilities. On the other hand there are little projects scattered around codeplex which do parts of the overall oauth functionalities such as signing and validation.

     

    I've decided on the latter for this example, using the Oauth.Manager Library avaialble on codeplex here.

     

    Include this as a reference in your project. Here is the C# code to push messages into the Jive Apps gateway:

     

    var oauth = new OAuth.Manager();
                oauth["consumer_key"] = "YOUR_CONSUMER_KEY";
                oauth["consumer_secret"] = "YOUR CONSUMER SECRET";



      //Build the JSON string that we will pass to the Apps gateway. In real life you might want to use a more robust library to create these
      //Such as JSON.Net, but for this example I'll just create a simple string.

                String strPost = @"{
                    ""items"":
                     [{
                       ""verb"" : [""http://activitystrea.ms/schema/1.0/favorite"", ""post""],
                        ""title"" : ""Bessie the cow was fed"",
                       ""body"" : ""Fred Flintstone fed Bessie the Cow"",
                   ""summary"" :  ""Video of Bessie the cow singing"",
                   ""mediaLink"": { ""url"" : ""http://www.youtube.com/watch?v=1BSbJ9eVVcs"" }
                     }]
                }";
                */


                StreamWriter myWriter = null;


    //Use this URL to post a message to the Activity Stream
                var appUrl = "https://gateway.jivesoftware.com/gateway/api/activity/v1/update/JIVE_INSTANCE_UUID/JIVE_APP_UUID/JIVE_USERID";

    //Use this URL to post something to the users What Matter:Actions tab
                //var appUrl = "https://gateway.jivesoftware.com/gateway/api/activity/v1/update/JIVE_INSTANCE_UUID/JIVE_APP_UUID/FROM_JIVE_USER?deliverTo=TO_JIVE_USER";

              var authzHeader = oauth.GenerateAuthzHeader(appUrl, "POST");
                var request = (HttpWebRequest)WebRequest.Create(appUrl);
                request.Method = "POST";
                request.PreAuthenticate = true;
                request.AllowWriteStreamBuffering = true;
                request.Headers.Add("Authorization", authzHeader);

    //If you do not include your app ID as a header, the post will fail.
                request.Headers.Add("X-Jive-App-Id", "JIVE_APP_UUID");

                request.ContentLength = strPost.Length;
                try
                {


                    myWriter = new StreamWriter(request.GetRequestStream());


                    myWriter.Write(strPost);


                }


                catch (Exception ex)
                {


                    //Do something about the exception


                }


                finally
                {


                    myWriter.Close();


                }


                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                        //Do something if you get an error code
                }
            }