无需用户名和密码即可验证SharePoint在线REST API

问题描述 投票:1回答:1

我正在尝试构建一个控制台应用程序,以从SharePoint列表中读取数据。

但是我不想通过用户凭据进行身份验证,而是使用令牌。这可能吗?

我尝试使用以下代码,但在尝试GetFormDigest时出现禁止的错误

        static void Main(string[] args)
        {
            string sConnStr = "https://sab/sites/DevT";
            Uri oUri = null;
            oUri = new Uri(sConnStr + "/_api/web/lists/getbytitle('sd')/GetItems");

            string sResult = string.Empty;

            string stringData = "{'query' : {'__metadata': { 'type': 'SP.CamlQuery' }, 'ViewXml':'<View><Query><Where><Eq><FieldRef Name =\"Title\"/><Value Type=\"Text\">HR</Value></Eq></Where></Query></View>'}}";

            HttpWebRequest oWebRequest = (HttpWebRequest)WebRequest.Create(oUri);
            oWebRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
            oWebRequest.Method = "POST";
            oWebRequest.Accept = "application/json;odata=verbose";
            oWebRequest.ContentType = "application/json;odata=verbose";
            oWebRequest.Headers.Add("X-RequestDigest", GetFormDigest());
            oWebRequest.ContentLength = stringData.Length;

            StreamWriter writer = new StreamWriter(oWebRequest.GetRequestStream());
            writer.Write(stringData);
            writer.Flush();

            WebResponse wresp = oWebRequest.GetResponse();
            using (StreamReader sr = new StreamReader(wresp.GetResponseStream()))
            {
                sResult = sr.ReadToEnd();
            }
        }

        public static string GetFormDigest()
        {
            string sFormDigest = null;
            string sConnStr = "https://sab/sites/DevT";
            Uri oUri = null;
            oUri = new Uri(sConnStr + "/_api/contextinfo");

            HttpWebRequest oWebRequest = HttpWebRequest.Create(oUri) as HttpWebRequest;
            oWebRequest.UseDefaultCredentials = true;
            oWebRequest.Method = "POST";
            oWebRequest.Accept = "application/json;odata=verbose";
            oWebRequest.ContentLength = 0;
            oWebRequest.ContentType = "application/json";
            string sResult;
            WebResponse sWebReponse = oWebRequest.GetResponse();

            using (StreamReader sr = new StreamReader(sWebReponse.GetResponseStream()))
            {
                sResult = sr.ReadToEnd();
            }

            var jss = new JavaScriptSerializer();
            var val = jss.Deserialize<Dictionary<string, object>>(sResult);
            var d = val["d"] as Dictionary<string, object>;
            var wi = d["GetContextWebInformation"] as Dictionary<string, object>;
            sFormDigest = wi["FormDigestValue"].ToString();

            return sFormDigest;

        } 
c# authentication sharepoint-online office365api
1个回答
0
投票

您可以使用外接程序身份验证来访问SharePoint数据。

您可以在下面查看我的测试演示。

https://social.msdn.microsoft.com/Forums/office/en-US/d33f5818-f112-42fb-becf-3cf14ac5f940/app-only-token-issue-unauthorized-access?forum=appsforsharepoint

© www.soinside.com 2019 - 2024. All rights reserved.