【问题标题】:Authenticate SharePoint online REST API without user name and password无需用户名和密码即可验证 SharePoint 在线 REST API
【发布时间】:2019-10-24 15:08:34
【问题描述】:

我正在尝试构建一个从 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】:

    您可以使用插件身份验证来访问 SharePoint 数据。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2018-09-25
      • 1970-01-01
      相关资源
      最近更新 更多