Search code examples
c#jsonrestjive

Unable to post a share using Jive REST API - getting 400 Bad Request error


I am trying to post a share on Jive using the /Shares REST API in .net using C#. However I am not able to do this and getting the following error:

"The remote server returned an error: (400) Bad Request."

Following is the code which I have written:

string response = string.Empty;

using(WebClient client = new WebClient())

{

   string strJiveShareURL = "https://<JiveURL>";

          strJiveShareURL += "/api/core/v3/shares";

   var SharedJSON = new AddShareJSON

   {

      participants = new string[] {"https://<JiveURL>/api/core/v3/people/{username}" },

      shared = "https://<<Content URL to be shared>>",

      content= new Content

                {

                  type = "text/html",

                  text = "This is a test share from SharePoint to Jive"                   
                }

}; 

   var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

   string shareJSON = serializer.Serialize(SharedJSON);

   Console.WriteLine("Setting Credentials:");

   string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("UID:PWD"));

   client.Headers[HttpRequestHeader.Authorization]= "Basic " + credentials;

   client.Headers[HttpRequestHeader.Accept] = "application/json";

   client.Headers[HttpRequestHeader.ContentType] = "application/json";

   //BypassCertificateError();

   response = client.UploadString(strJiveShareURL, "POST", shareJSON);

   Console.WriteLine("Response:" + response);

   Console.ReadLine();



}

and following is the JSON which is created for posting the share:

{

  "content": {

                "type":"text/html",

                "text":"This is a test share from SharePoint to Jive"

            },

           "participants": ["https://<<Jive URL>>/api/core/v3/people/<<username>>"],

           "Shared":"https://<<URL of the Content To be Shared>>"

}

Please let me know if there is anything which I have been doing incorrectly.


Solution

  • I figured this out myself, I was getting the error because I was passing an invalid URI object to the shared parameter of the Share REST endpoint. The shared parameter requires a content URI in the form of

    http://[[JiveURL]]/api/core/v3/contents/[[ContentID]]

    Earlier I was trying to pass URLs external to Jive resulting in the bad request error.