client.PostAsync("Url", null) Gets 401 unauthorized error REST API

nderonhnderonh Member ✭✭
edited January 25 in Xamarin.Forms

I am developing a cross mobile app using Xamarin and I am trying to post some items to a SharePoint List.

I have created a windows form app just to test the SharePoint Rest API and it is working good, I have the following code:

        var username = "user";
        var password = "pass";
        var domain = "domain";

        var handler = new HttpClientHandler
        {
            Credentials = new NetworkCredential(username, password, domain)
        };

        var client = new HttpClient(handler);

        HttpResponseMessage content = client.PostAsync("/mysite/_api/contextinfo", null).Result;

the content variable (HttpResponseMessage) returns me a 200 Status code, which is OK and everything is working.

However I have the same code in my Xamrin Froms but it is not working it throws me a 401 unauthorized error

this is my Xamarin Code

    private async void BtnRestApi_OnClicked(object sender, EventArgs e)
    {
         var username = "user";
         var password = "pass";
         var domain = "domain";

         var handler = new HttpClientHandler
         {
             Credentials = new NetworkCredential(username, password, domain)
         };

          var client = new HttpClient(handler);

         HttpResponseMessage content = await client.PostAsync("/mysite/_api/contextinfo", null).Result;
    } 

Answers

  • StandaMikesStandaMikes USMember ✭✭

    I cannot reproduce it on my machine. However, why do you use await and Result at the same the? Couldn`t be that the issue?

  • batmacibatmaci DEMember ✭✭✭✭✭

    open fiddler and run your application in UWP local to observer what is posted. it should be something wrong with your request.

  • jezhjezh Member, Xamarin Team Xamurai

    Have you got the solution at last?

  • nderonhnderonh Member ✭✭
    edited February 12

    Hi everyone thanks all for the help, I have come around with another solution:

    private async Task<string> DigestValue()
        {
            string result = string.Empty;
            Uri uri = new Uri("//mysite/_api/contextinfo");
            HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(uri);
    
            wreq.Credentials = new NetworkCredential("username", "password", "domain"); 
    
            wreq.Method = "POST";
            wreq.Accept = "application/json; odata=verbose";
            wreq.ContentType = "application/json; odata=verbose";
    
            wreq.GetRequestStream();
            WebResponse wresp = wreq.GetResponse();
            using (StreamReader sr = new StreamReader(wresp.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }
    
    
            var json = JsonConvert.DeserializeObject<dJson>(result);
            var digestToket = json.Json.DigestJsonCollection.Title;
    
            return digestToket;
        }
    

    this is working perfectly fine

Sign In or Register to comment.