SharePoint 2013 文档库使用 OpenBinaryDirect 方法我得到远程服务器 401 未经授权的错误?

问题描述:

当我使用 OpenBinaryDirect 方法从 SharePoint 2013 文档库检索文件时,出现远程服务器 401 未授权错误?你能帮我解决这个问题吗

When I am retrieving the file from SharePoint 2013 Document Library by using OpenBinaryDirect method I got remote server 401 unauthorized error? Could you please help me how to rectify the problum

通过使用提供在线凭证代码中的 SharePointOnlineCredentials(username,password) 类

Provide online-credentials by using SharePointOnlineCredentials(username,password) class in your code

代码:

string username="xxx";

string username="xxx";

string password="xx";

string password="xx";

SecureString ss=new SecureString();

SecureString ss=new SecureString();

foreach(密码中的字符 c)ss.AppendChar(c)

foreach(char c in password) ss.AppendChar(c)

clientcontext.credentials = SharePointOnlineCredentials(username,ss);

clientcontext.credentials = SharePointOnlineCredentials(username,ss);

Web web = clientContext.Web;

Web web = clientContext.Web;

List list = web.Lists.GetById(new Guid("xxxxxxxxxx"));

List list = web.Lists.GetById(new Guid("xxxxxxxxxx"));

var data = new CamlQuery() { ViewXml = "query" };

var data = new CamlQuery() { ViewXml = "query" };

Microsoft.SharePoint.Client.ListItemCollection items_attachments = list.GetItems(data);

Microsoft.SharePoint.Client.ListItemCollection items_attachments = list.GetItems(data);

clientContext.Load(items_attachments);

clientContext.Load(items_attachments);

clientContext.ExecuteQuery();

clientContext.ExecuteQuery();

foreach(items_attachments 中的 Microsoft.SharePoint.Client.ListItem 列表项){

foreach (Microsoft.SharePoint.Client.ListItem listitem in items_attachments) {

clientContext.Load(listitem, i => i.File);

clientContext.Load(listitem, i => i.File);

clientContext.ExecuteQuery();

clientContext.ExecuteQuery();

var fileRef = listitem.File.ServerRelativeUrl;

var fileRef = listitem.File.ServerRelativeUrl;

FileInformation fileinfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);

FileInformation fileinfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);

流 fl = fileinfo.Stream;

Stream fl = fileinfo.Stream;

byte[] s = ReadFully(fl);

byte[] s = ReadFully(fl);

}

public static byte[] ReadFully(流输入){

public static byte[] ReadFully(Stream input) {

        byte[] buffer = new byte[16 * 1024];

        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }