繁体   English   中英

REST Search API远程服务器返回错误:(401)未经授权

[英]REST Search API The remote server returned an error: (401) Unauthorized

我通过使用本教程创建了一个控制台应用程序:

http://blogs.msdn.com/b/kaevans/archive/2014/03/02/building-a-sharepoint-app-as-a-timer-job.aspx

当然有一些区别,在我的控制台应用程序中,我想对搜索api进行调用。

阅读本文:授予应用程序主体权限而不是像示例中那样使用租户管理,我使用了此方法:

 <AppPermissionRequests>
    <AppPermissionRequest Scope="https://sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal" />
  </AppPermissionRequests>

然后我去了“ _layouts / AppInv.aspx”。 并使用上面的客户端ID和xml注册了应用。

然后,我单击“信任它”。

我的app.config是这样的:

<xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Sites"
             type="System.Configuration.NameValueSectionHandler"/>
  </configSections>
  <appSettings>
    <add key="ClientId" value="xx-1c6c-45e7-8a2a-7364a705c836"/>
    <add key="ClientSecret" value="+xx="/>
  </appSettings>
  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <Sites>
    <add key="site1" value="https://xx.sharepoint.com/sites/developersitecollection"/>
  </Sites>
</configuration>

然后我的控制台应用程序失败,在getresponse上显示未经授权的异常

static void Main(string[] args)
        {
            var config = (NameValueCollection)ConfigurationManager.GetSection("Sites");


            foreach (var key in config.Keys)
            {
                Uri siteUri = new Uri(config.GetValues(key as string)[0]);

                using (ClientContext clientContext = new ClientContext(siteUri))
                {
                    string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
                    string accessToken =
                        TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,
                        siteUri.Authority, realm).AccessToken;

                    HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(siteUri +"/_api/search/query?querytext=%27*.pptx%27");
                    endpointRequest.Method = "GET";
                    endpointRequest.Accept = "application/json;odata=verbose";
                    endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken);
                    HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
                    StreamReader reader = new StreamReader(endpointResponse.GetResponseStream());
                    var searchXml = new XmlDocument();
                    searchXml.LoadXml(reader.ReadToEnd()); 

                }
            }
        }

我想知道问题可能是什么

QueryAsUserIgnoreAppPrincipal权限完全按照其说的进行操作...它以当前用户身份查询搜索,而忽略了应用程序权限。 您的代码获得仅应用程序权限,该权限不提供用户信息。 我希望在这种情况下未经授权的401。

请记住,搜索查询是根据用户进行安全性修剪的。 您可能会尝试对仅应用程序的上下文使用“租户读取”权限,删除QueryAsUserIgnoreAppOnly,但是我还没有尝试过看看是否会起作用。 更好的选择可能只是使用SharePointOnlineCredentials类来指定具有足够权限来访问内容的用户(如果该用户无权访问该内容,则该内容将不会出现在搜索结果中)。

http://blogs.msdn.com/b/kaevans/archive/2014/02/23/call-o365-using-csom-with-a-console-application.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM