我正在使用Visual Studio 2013创建一个.net web api(.net 4.5),调用Google.Apis.YouTube.v3来搜索视频。当我使用IIS Express(例如http:// localhost:12345 /)时,一切正常,但是一旦我改为使用本地IIS(例如http:// localhost / HelloWorldWebApi),它就会抛出一个System.UnauthorizedAccessException {“访问“Google.Apis.Auth”路径被拒绝。“}。
我在网上搜索了几个小时,一无所获。
我的开发环境: Windows 8.1,Visual Studio 2013,IIS 8.5
代码:
public IEnumerable<MyVideo> Get(string search)
{
try
{
string resourceName = "HelloWorldWebApi.client_secret.json";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
Credential(stream).Wait();
}
YouTubeService youtube = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = _credential,
ApplicationName = "HelloWorldWebApiTest"
});
SearchResource.ListRequest listRequest = youtube.Search.List("snippet");
listRequest.Q = search;
listRequest.MaxResults = 5;
listRequest.Type = "video";
SearchListResponse searchResponse = listRequest.Execute();
List<MyVideo> videos = new List<MyVideo>();
foreach (SearchResult result in searchResponse.Items)
{
videos.Add(new MyVideo(result.Snippet.Title));
}
return videos;
}
catch (Exception ex)
{
throw ex;
}
}
private async Task Credential(Stream stream)
{
_credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.YoutubeReadonly },
"user", CancellationToken.None);
}
提前感谢所有帮助!
答案 0 :(得分:0)
可能是因为这个问题的原因是IIS Express在另一个帐户(您登录的帐户)下运行,然后是IIS即时(取决于池配置,但默认是ApplicationPoolIdentity)?我认为IIS帐户无权访问包含Google.Apis.Auth的资源。