mercredi 28 novembre 2018

c# script Sharepoint API giving 401 Error on remote server

' '

I am using below code for sharepoint authentication which works perfectly fine on local machine but when I deploy this package on server it is giving 401 error.

class Program {

    static void Main(string[] args)
    {
        //Delcaring Required variable (Actual variable values have been changed to dummy one)
        var webUri = new Uri("https://blahblah.sharepoint.com");  
        string proxy = "http://abc.abc.com:1234/";
        const string userName = "abc@blahblah.com";
        const string password = "123";
        string SalesPOV_GUID = "6319bd1f-7f-4ffd-95dc-a992afc4da10";
        string Clients_GUID = "ojfoeiawe-3abcc6-41cd-be23-cf6043671d53";

        //Creating a secured sring for SharepointOnline Credentials
        var securePassword = new SecureString();
        foreach (var c in password)
        {
            securePassword.AppendChar(c);
        }

        //Setting up credentials for Sharepoint
        var credentials = new SharePointOnlineCredentials(userName, securePassword);

        //Makiing a Call
        using (var client = new WebClient())
        {


            try
            {
                //setting up proxy
                System.Net.WebProxy wp = new System.Net.WebProxy();
                Uri newUri = new Uri(proxy);
                wp.Address = newUri;
                client.Proxy = wp;
            }
            catch (WebException e) //In package we need to fail the package on catching exception.
            {
                string pageContent = new StreamReader(e.Response.GetResponseStream()).ReadToEnd().ToString();
            }
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            client.Credentials = credentials;
        }
    }

}




Aucun commentaire:

Enregistrer un commentaire