c#脚本Sharepoint API在远程服务器上给出401错误

时间:2018-11-28 08:42:37

标签: c# web sharepoint sharepoint-rest-api

我正在使用以下代码进行共享点身份验证。它在本地计算机上可以正常工作,但是当我在服务器上部署此程序包时却出现401错误。

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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

检查您的共享点网站是否将物理路径设置为“通过”身份验证。