访问"Google.Apis.Auth"路径被拒绝

 sdx3418153 发布于 2023-02-13 16:48

我正在用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 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 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);
    }

在此先感谢您的所有帮助!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有