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 ( ) 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 = @"{
                       ""verb"" : ["""", ""post""],
                        ""title"" : ""Bessie the cow was fed"",
                       ""body"" : ""Fred Flintstone fed Bessie the Cow"",
                   ""summary"" :  ""Video of Bessie the cow singing"",
                   ""mediaLink"": { ""url"" : """" }

                StreamWriter myWriter = null;

    //Use this URL to post a message to the Activity Stream
                var appUrl = "";

    //Use this URL to post something to the users What Matter:Actions tab
                //var appUrl = "";

              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;

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



                catch (Exception ex)

                    //Do something about the exception





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