Error on API CreateLink?

  • 1
  • Question
  • Updated 2 years ago

Hello,

I would create a link on a folder then I write:


string link = null;

String uri = string.Format("https://{0}/sf/v3/Items({1})/Link";, sf.GetHostname(token), folderId);

HttpWebRequest request = WebRequest.CreateHttp(uri);

sf.addAuthorizationHeader(request, token);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (var reader = new StreamReader(response.GetResponseStream()))

{

    link = reader.ReadToEnd();

}


The function request.GetResponse() crashs on an exception error 405 : Method not allowed.

What is the problem?

Thank,

Sylver


Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

Posted 2 years ago

  • 1
Photo of Eliezer Encarnacion

Eliezer Encarnacion, Software Engineer

  • 733 Posts
  • 102 Reply Likes
Sylver,

The request should be a POST and should include a Json body with the creation parameters.

Thanks!
Eli
Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

Hello Eli,

Thank you for this reply.

What are the creation parameters?

I don't see it in ShareFile ressources (https://api.sharefile.com/rest/docs/resource.aspx?name=Items)

Sylver


Photo of Eliezer Encarnacion

Eliezer Encarnacion, Software Engineer

  • 733 Posts
  • 102 Reply Likes
It's there, right under the "Create Link" request


POST https://account.sf-api.com/sf/v3/Items(parentid)/Link

{
"Name":"Link Name",
"Description":"Description",
"Uri":"https://server/path"
}
(Edited)
Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

It is working!

How I get a link at the end?


String body = reader.ReadToEnd();

JObject newLink = JObject.Parse(body);

link = newLink[???].ToString();

      

Sylvère         

Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

Hello Eli,

Sorry because not that I was looking for to create a link.

I wanted to get a link as https://domain.sharefile.eu/e-s62f110e555eff239 and the finction CreateLink don't allow to give this link.

Is there an function who allows to it?

Thanks!

Photo of Eliezer Encarnacion

Eliezer Encarnacion, Software Engineer

  • 733 Posts
  • 102 Reply Likes
Hey,

In that case you're looking to create a Share Link. Refer to the Shares documentation here: http://api.sharefile.com/rest/docs/resource.aspx?name=Shares
The "Create Share" endpoint is the one you're looking for, it has all the required parameters there, let me know if you have any questions.

Eli
Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

Hello Eli,

How to get an only share link without emails? I write the follown function but it crashs.


String uri = string.Format("https://{0}/sf/v3/Shares?notify=false";, getHostname());

HttpWebRequest request = WebRequest.CreateHttp(uri);

addAuthorizationHeader(request);

Dictionary<string, object> parameters = new Dictionary<string, object>();

parameters.Add("ShareType", "Send");

parameters.Add("Title", "CreateSendShare");

parameters.Add("Items", new string[] { "Id:" + itemId.ToString()} );

//parameters.Add("Recipients", null);

if (expirationDays > 0) parameters.Add("ExpirationDays", expirationDays);

parameters.Add("IsViewOnly", true);

parameters.Add("RequireLogin", requireLogin);

parameters.Add("RequireUserInfo", requireUserInfo);

parameters.Add("MaxDownloads", -1);

parameters.Add("UseStreamIDs", false);

string json = JsonConvert.SerializeObject(parameters);

request.Method = "POST";

request.ContentType = "application/json";

using (var writer = new StreamWriter(request.GetRequestStream()))

{

   writer.Write(json);

}


HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (var reader = new StreamReader(response.GetResponseStream()))

{

   String body = reader.ReadToEnd();

   JObject share = JObject.Parse(body);

   sendShare = share["Uri"].ToString();         

}


Sylver

Photo of Eliezer Encarnacion

Eliezer Encarnacion, Software Engineer

  • 733 Posts
  • 102 Reply Likes
The items parameter is malformed. It should not be a list of Item Ids, but a list of Item objects, e.g [{"Id": "itemId"}, {"Id":"ItemId2"}]. 
Have you considered using the SDK? https://github.com/citrix/ShareFile-NET 

It uses strongly typed models for the requests, which makes them easier to construct.

Thanks,
Eli
Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

No I write a code as the c# code sample from https://api.sharefile.com/rest/samples/csharp.aspx.

For your syntax, what is the variable declaration?

Or, please give me a good syntax for my code above?


Thank you.

Photo of Eliezer Encarnacion

Eliezer Encarnacion, Software Engineer

  • 733 Posts
  • 102 Reply Likes
Each Item must be its own dictionary with the "Id", "Value" pair
Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

Yes but I don't see how to write?

I have

Dictionary<string, object> parameters = new Dictionary<string, object>();

parameters is the 1-dimension dictionary with the key and value pair so

I must write :

parameters.Add("Id", itemId1)

parameters.Add("Id", ItemId2)

...

?


Photo of Eliezer Encarnacion

Eliezer Encarnacion, Software Engineer

  • 733 Posts
  • 102 Reply Likes
Sylver,

I can't say I know either. They way I'd go about it is using a strongly typed model (or anonymous object) since it would be easier to map that to json. For example

var shareParams = new
{
    Items = new[] { new { Id = "id1"}, new {Id =" id2"} },
    ShareType = "Send",
    //Rest of parameters...
};

string json = JsonConvert.SerializeObject(shareParams);


That should do it using anonymous objects, but if you wanted to create actual models for the Share and Item objects, it would work as well.

Thanks!
Eli
(Edited)
Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

Thank you.

I have modified:

var parameters = new

{

ShareType = "Send",

Title = "CreateSendShare",

Items = new[] { new { Id = itemId } }, // { new { Id = "id1" }, new { Id = " id2" } },

ExpirationDate = "2018-09-30",

IsViewOnly = true,

RequireLogin = false,

RequireUserInfo = requireUserInfo,

MaxDownloads = -1,

UseStreamIDs = false

};

string json = JsonConvert.SerializeObject(parameters);


The code give:

https://domain.sf-api.com/sf/v3/Shares?notify=false
{"ShareType":"Send","Title":"CreateSendShare","Items":[{"Id":"fi29e762-1e1e-a3be
-d14a-d32925030dd1"}],"ExpirationDate":"2018-09-30","IsViewOnly":true,"RequireLo
gin":false,"RequireUserInfo":false,"MaxDownloads":-1,"UseStreamIDs":false}


I have always a error mesage : Method not authorized (405).

I haven't filled the récipients because I would get a link without send mail.

Is the code good?

Photo of Eliezer Encarnacion

Eliezer Encarnacion, Software Engineer

  • 733 Posts
  • 102 Reply Likes
405 means the request is not a POST. I'd double check the code is handling that correctly
Photo of Sylver

Sylver

  • 16 Posts
  • 1 Reply Like

Hi Eli,

I have found two problems:

1. This wasn't the right Id 

2. I remove "IsViewOnly":true

--> it is working!


Thank you a lot! I have just started and I have just discovered the possibilities of testing the requests in online via http://api.sharefile.com/ :)

Sylver.