We are using Microsoft technologies for a number of our services and products. Most recently our Windows Phone app for Jive.

 

Using the Jive REST API from a .Net/C# application is actually very straightforward, so I am going to outline a basic example in this post.

Assumptions:

  • Jive system that supports the v3 api. I am using Jive Cloud in this example
  • .Net 4.5(.1) based project. I am using Visual Studio 2013 Update 2

 

In this example we are going to retrieve a person object from Jive (Jive REST Rest API v3.7 → Person entity)

 

The data model:

The person object

    public class Person
    {
        public string displayName { get; set; }
        public int followerCount { get; set; }
        public int followingCount { get; set; }
        public int id { get; set; }
        public string location { get; set; }
        public string status { get; set; }
        public string thumbnailId { get; set; }
        public string thumbnailUrl { get; set; }
        public DateTime updated { get; set; }
        public List<ProfileEntry> emails { get; set; }
        public JivePerson jive { get; set; }
        public Name name { get; set; }
        public List<ProfileEntry> phoneNumbers { get; set; }
        public List<ProfileEntry> photos { get; set; }
        public DateTime published { get; set; }
        public List<string> tags { get; set; }
        public JiveResource resources { get; set; }

    }

The JivePerson object that holds the Jive specific information about a person

    public class JivePerson
    {
        public bool enabled { get; set; }
        public bool external { get; set; }
        public bool externalContributor { get; set; }
        public bool federated { get; set; }
        public DateTime lastProfileUpdate { get; set; }
        public Level level { get; set; }
        public string locale { get; set; }
        public List<ProfileEntry> profile { get; set; }
        public bool sendeable { get; set; }
        public bool termsAndConditionsRequired { get; set; }
        public string timeZone { get; set; }
        public string username { get; set;}
        public bool viewContent { get; set; }
        public bool visible { get; set; }
    }

 

Additional objects for statuslevel, profile entries and names

    public class Level
    {
        public string description { get; set; }
        public string imageURI { get; set; }
        public string name { get; set; }
        public int points { get; set; }
    }


    public class ProfileEntry
    {
        public string jive_label { get; set; }
        public string value { get; set; }
        public string type { get; set; }
        public bool primary { get; set; }
    }
    public class Name
    {
        public string familyName { get; set; }
        public string formatted { get; set; }
        public string givenName { get; set; }
    }

 

 

Now that we have the datamodel for our person, let's retrieve a person via the rest api. In our example we are going to use the simplest call by getting the logged in user. I've hardcoded the necessary Urls in this example.

 

string jiveServer = "https://community.jivesoftware.com";
Person person = await JiveDataSource.GetPersonAsync(jiveServer + "/api/core/v3/people/@me");

 

The asynchronous method gets the Json describing the person using the ExecuteAbsolute method in the JiveRequest class.

Afterwards the JsonConvert.DeserializeObject method from the Json.net libary is used to create a person object from the Json.

 

   public static async Task<Person> GetPersonAsync(string url)
        {
            JiveRequest myJiveRequest = new JiveRequest();


            string jiveCommunityJSON = await myJiveRequest.ExecuteAbsolute(url);
            Person myPerson = JsonConvert.DeserializeObject<Person>(jiveCommunityJSON);
            return myPerson;
        }

 

Finally here is the JiveRequest.ExecuteAbsolute method. Again values are hardcoded in this example

 

        public async Task<string> ExecuteAbsolute(string url)
        {
          
            string jiveCommunityUrl = "https://community.jivesoftware.com";
           //This example uses basic auth. OAuth based authentication is also possible

            string userName = "myuser";
            string password = "mypassword";

            System.Net.Http.HttpClientHandler jiveHandler = new System.Net.Http.HttpClientHandler();

          //Setting credentials for our request. This needs to be done for every request as there are no persistent sessions for the REST Api
            NetworkCredential myCredentials = new NetworkCredential(userName, password);
            myCredentials.Domain = jiveCommunityUrl + "/api/core/v3";
          //Getting our credentials in Base64 encoded format
            string cre = String.Format("{0}:{1}", userName, password);
              byte[] bytes = Encoding.UTF8.GetBytes(cre);
           string base64 = Convert.ToBase64String(bytes);
          //Set credentials and make sure we are pre-authenticating our request
            jiveHandler.Credentials = myCredentials;
            jiveHandler.PreAuthenticate = true;
            jiveHandler.UseDefaultCredentials = true;

           httpClient = new System.Net.Http.HttpClient(jiveHandler);
            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", base64);


            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, url);

            System.Net.Http.HttpResponseMessage activityResponse = await httpClient.SendAsync(requestMessage);
            String myActivityResponse = await activityResponse.Content.ReadAsStringAsync();
          //Remove the string Jive includes in every response from the REST API
            string cleanResponseActivities = myActivityResponse.Replace("throw 'allowIllegalResourceCall is false.';", "");


            return cleanResponseActivities;


        }