使用HTTPClient和Sharepoint 2013 REST API获取文档

时间:2013-11-01 13:51:28

标签: asp.net rest sharepoint sharepoint-2013

我正在使用.NET Web应用程序。我正在尝试连接到Sharepoint Online Premise以获取"文档库中的文档"通过使用HTTPClient和Sharepoint 2013 REST API。

我在浏览器中为此测试了以下休息服务,

   https://site_url/_vti_bin/ListData.svc/Documents

给出了特定网站文档库中存在的所有文档的MetaData。

现在,当我尝试通过添加对服务URL的引用并使用以下代码以编程方式执行相同操作时,

TestSiteDataContext ctx = new TestSiteDataContext(new Uri("https://site url/_vti_bin/ListData.svc"));
ctx.Credentials = new NetworkCredential("test@test.onmicrosoft.com", "test");
var data = ctx.Documents.ToList();

我收到以下异常: "请求已中止:无法创建SSL / TLS安全通道。" 堆栈跟踪如下:

at System.Net.HttpWebRequest.GetResponse()
   at System.Data.Services.Client.QueryResult.Execute()
   at System.Data.Services.Client.DataServiceRequest.Execute[TElement](DataServiceContext context, QueryComponents queryComponents)
   at System.Data.Services.Client.DataServiceQuery`1.Execute()
   at System.Data.Services.Client.DataServiceQuery`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at SPO_AccessAWebService.Program.Main(String[] args) in Location\Program.cs:line 35
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

我已经通过各种articles/blogs来找到解决方案。但是找不到任何解决办法。 我可以通过

看到各种样本显示身份验证
ctx.Credentials = CredentialCache.DefaultNetworkCredentials;

这也是同样的错误。

是否有其他方法可以对SharePoint 2013 REST API进行身份验证?

有人可以为我提供解决此问题的一些见解吗?

1 个答案:

答案 0 :(得分:0)

根据您报告的异常消息,SSL证书似乎对该站点无效。我经常在使用自签名分数的开发或测试环境中看到此消息。如果在浏览器中访问URL时必须单击警告消息,则应该能够判断证书是否无效。要解决此问题,您可以尝试通过http连接,如果不关心传输安全性,获取或安装有效证书,或将当前证书导入当前用户或本地计算机的受信任的根权限存储。

相关问题