[英]REST Search API The remote server returned an error: (401) Unauthorized
I have created a console application by using this tutorial: 我通过使用本教程创建了一个控制台应用程序:
http://blogs.msdn.com/b/kaevans/archive/2014/03/02/building-a-sharepoint-app-as-a-timer-job.aspx http://blogs.msdn.com/b/kaevans/archive/2014/03/02/building-a-sharepoint-app-as-a-timer-job.aspx
With some differences of course, in my console app, I want to make a rest call to the search api. 当然有一些区别,在我的控制台应用程序中,我想对搜索api进行调用。
Read this: Giving the App Principal Permissions Instead of using tenant manage as in the example I used this: 阅读本文:授予应用程序主体权限而不是像示例中那样使用租户管理,我使用了此方法:
<AppPermissionRequests>
<AppPermissionRequest Scope="https://sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal" />
</AppPermissionRequests>
Then I went to “_layouts/AppInv.aspx”. 然后我去了“ _layouts / AppInv.aspx”。 And registered the app using the client id and the xml above.
并使用上面的客户端ID和xml注册了应用。
Then I clicked on Trust it. 然后,我单击“信任它”。
My app.config is like this: 我的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>
Then my console app fails with Unauthorized Exception on the getresponse 然后我的控制台应用程序失败,在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());
}
}
}
I wonder what the problem might be 我想知道问题可能是什么
QueryAsUserIgnoreAppPrincipal permission does exactly what it says... it queries search as the current user, ignoring the app permission. QueryAsUserIgnoreAppPrincipal权限完全按照其说的进行操作...它以当前用户身份查询搜索,而忽略了应用程序权限。 Your code gets an app-only permission, which does not provide user information.
您的代码获得仅应用程序权限,该权限不提供用户信息。 I would expect the 401 unauthorized in this scenario.
我希望在这种情况下未经授权的401。
Remember that search queries are security-trimmed based on a user. 请记住,搜索查询是根据用户进行安全性修剪的。 You might try using Tenant Read permissions for an app-only context, removing the QueryAsUserIgnoreAppOnly, but I haven't tried to see if that will work.
您可能会尝试对仅应用程序的上下文使用“租户读取”权限,删除QueryAsUserIgnoreAppOnly,但是我还没有尝试过看看是否会起作用。 A better option might be simply using the SharePointOnlineCredentials class to specify a user that has sufficient permissions to access the content (if the user does not have access to the content, the content will not appear in search results).
更好的选择可能只是使用SharePointOnlineCredentials类来指定具有足够权限来访问内容的用户(如果该用户无权访问该内容,则该内容将不会出现在搜索结果中)。
http://blogs.msdn.com/b/kaevans/archive/2014/02/23/call-o365-using-csom-with-a-console-application.aspx 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.