微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Sharepoint REST API客户端控制台应用程序C#

如何解决Sharepoint REST API客户端控制台应用程序C#

我正在寻找从C#控制台应用程序使用Sharepoint REST API的示例(请更详细地阅读Sharepoint列表)。 MS网站上有一些教程,但我认为它们不完整。例如,此代码显示如何获取访问令牌,因此我找不到任何演示代码https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints

本教程正是我所需要的,但是代码不起作用:https://blog.vgrem.com/2015/04/04/consume-sharepoint-online-rest-service-using-net/

private static CookieContainer GetAuthCookies(Uri webUri,string userName,string password)
        {
            var securePassword = new securestring();
            foreach (var c in password) { securePassword.AppendChar(c); }
            var credentials = new SharePointOnlineCredentials(userName,securePassword);
            var authCookie = credentials.GetAuthenticationCookie(webUri);
            var cookieContainer = new CookieContainer();
            cookieContainer.SetCookies(webUri,authCookie);
            return cookieContainer;
        }

这行var authCookie = credentials.GetAuthenticationCookie(webUri);无效。即使所有的webUri,userName,password正确,它也始终返回null。

有人可以指出我正确的方向还是给我一个客户代码示例?该服务器正在运行Sharepoint 2013。

解决方法

我的测试代码供您参考:

static void Main(string[] args)
        {
            HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("http://sp/_api/web");
            endpointRequest.Method = "GET";
            endpointRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED","f");
            endpointRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
            HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
            try
            {
                WebResponse webResponse = endpointRequest.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();//results
                responseReader.Close();
                Console.WriteLine(response);
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message); Console.ReadLine();
            }
        }

或者您可以使用此凭据:

var username = "administrator";
             var password = "P@ssw0rd";
             var domain = "contoso";
             endpointRequest.Credentials=new System.Net.NetworkCredential(username,password,domain);

SharePoint 2013不需要生成访问令牌。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。