【问题标题】:Youtube Video Upload on web with Google API and scope youtube.upload使用 Google API 和范围 youtube.upload 在网络上上传 Youtube 视频
【发布时间】:2019-03-10 15:43:00
【问题描述】:

我正在尝试在用 MVC 开发的 Youtube 上上传一个视频,但它在 Web 应用程序中失败并出现以下错误,而它在本地系统中运行良好。 托管在 AWS 上的服务器。

无法启动带有“https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=abcd&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.upload%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive”的浏览器进行授权。有关详细信息,请参阅内部异常。

寻求帮助

【问题讨论】:

    标签: c# asp.net-mvc google-api youtube-api google-oauth


    【解决方案1】:

    我将假设您使用的是 Google APIs .net 客户端库。有几种类型的客户端,有 Google 登录 Web 客户端和 Native 应用程序客户端。

    用于验证两个应用程序的代码不同。当您使用本机客户端进行身份验证时,您会在本地 PC 上生成 Web 浏览器。对于托管在网站上的 Web 应用程序,必须在用户客户端计算机上打开浏览器。

    您在此处看到的错误通常是因为您正在托管它,并且它试图在服务器本身而不是用户 clinet 上打开 Web 浏览器。 您应该遵循本教程web-applications-aspnet-mvc

    添加您自己的 FlowMetadata 实现。

    using System;
    using System.Web.Mvc;
    
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Auth.OAuth2.Flows;
    using Google.Apis.Auth.OAuth2.Mvc;
    using Google.Apis.Drive.v2;
    using Google.Apis.Util.Store;
    
    namespace Google.Apis.Sample.MVC4
    {
        public class AppFlowMetadata : FlowMetadata
        {
            private static readonly IAuthorizationCodeFlow flow =
                new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                    {
                        ClientSecrets = new ClientSecrets
                        {
                            ClientId = "PUT_CLIENT_ID_HERE",
                            ClientSecret = "PUT_CLIENT_SECRET_HERE"
                        },
                        Scopes = new[] { DriveService.Scope.Drive },
                        DataStore = new FileDataStore("Drive.Api.Auth.Store")
                    });
    
            public override string GetUserId(Controller controller)
            {
                // In this sample we use the session to store the user identifiers.
                // That's not the best practice, because you should have a logic to identify
                // a user. You might want to use "OpenID Connect".
                // You can read more about the protocol in the following link:
                // https://developers.google.com/accounts/docs/OAuth2Login.
                var user = controller.Session["user"];
                if (user == null)
                {
                    user = Guid.NewGuid();
                    controller.Session["user"] = user;
                }
                return user.ToString();
    
            }
    
            public override IAuthorizationCodeFlow Flow
            {
                get { return flow; }
            }
        }
    }
    

    FlowMetadata 是一个抽象类,其中包含您自己的用于检索用户标识符和您正在使用的 IAuthorizationCodeFlow 的逻辑。

    在上面的示例代码中,使用正确的范围、客户端机密和数据存储创建了一个新的 GoogleAuthorizationCodeFlow。考虑添加您自己的 IDataStore 实现,例如您可以编写一个使用 EntityFramework 的实现。

    实现您自己的使用 Google API 服务的控制器。以下示例使用 DriveService:

    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    
    using Google.Apis.Auth.OAuth2.Mvc;
    using Google.Apis.Drive.v2;
    using Google.Apis.Services;
    
    using Google.Apis.Sample.MVC4;
    
    namespace Google.Apis.Sample.MVC4.Controllers
    {
        public class HomeController : Controller
        {
            public async Task IndexAsync(CancellationToken cancellationToken)
            {
                var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
                    AuthorizeAsync(cancellationToken);
    
                if (result.Credential != null)
                {
                    var service = new DriveService(new BaseClientService.Initializer
                        {
                            HttpClientInitializer = result.Credential,
                            ApplicationName = "ASP.NET MVC Sample"
                        });
    
                    // YOUR CODE SHOULD BE HERE..
                    // SAMPLE CODE:
                    var list = await service.Files.List().ExecuteAsync();
                    ViewBag.Message = "FILE COUNT IS: " + list.Items.Count();
                    return View();
                }
                else
                {
                    return new RedirectResult(result.RedirectUri);
                }
            }
        }
    }
    

    实现您自己的回调控制器。实现应该是这样的:

    using Google.Apis.Sample.MVC4;
    
    namespace Google.Apis.Sample.MVC4.Controllers
    {
        public class AuthCallbackController : Google.Apis.Auth.OAuth2.Mvc.Controllers.AuthCallbackController
        {
            protected override Google.Apis.Auth.OAuth2.Mvc.FlowMetadata FlowData
            {
                get { return new AppFlowMetadata(); }
            }
        }
    }
    

    您需要将示例从 google drive 更改为 Youtube。

    【讨论】:

    • 感谢 DalmTo,我们将根据您的建议进行处理。然而到目前为止,我们尝试了下面的代码,它在本地主机上而不是在 Web 服务器上工作,请你检查一下。 : UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId =, ClientSecret = } , scopes , "user" , CancellationToken.None, new FileDataStore("Drive.Api. Auth.Store")).Result;
    猜你喜欢
    • 2019-08-08
    • 2012-10-27
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 2012-12-23
    • 1970-01-01
    相关资源
    最近更新 更多