我正在用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 IEnumerableGet(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 videos = new List (); 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); }
在此先感谢您的所有帮助!