【问题标题】:Google Developer API V3 for video upload in YouTube C#.net用于在 YouTube C#.net 中上传视频的 Google Developer API V3
【发布时间】:2015-08-25 15:35:33
【问题描述】:

我一直使用谷歌著名的 API V2 从我的网络应用程序上传 YouTube 上的视频,因为 API 已经升级到 V3,我无法通过相同的代码上传相同的视频。

我尝试过使用 V3 将视频上传到 YouTube 的新应用程序,但有很多东西以前有,但在 V3 中不可用

旧: 来自:Google.Gdata.YouTube.Youtubeservice

YouTubeService service = new YouTubeService(applicationName,developerKey);
service.setUserCredentials(googleEmail, googleEmailPassword);

新:来自:Google.Apis.Youtube.V3.YoutubeService

YouTubeService service = new YouTubeService();` -- its doesn't have the set credentials

我已经使用了YouTube Data API .NET Code Samples,但它并没有多大用处,因为它是一个控制台应用程序。

【问题讨论】:

    标签: c# youtube google-api youtube-api google-api-dotnet-client


    【解决方案1】:

    您不能再使用登录名和密码了。您需要使用 Oauth2 对用户进行身份验证。如果是 Web 或控制台应用程序,则代码相同。

    /// <summary>
    /// Authenticate to Google Using Oauth2
    /// Documentation https://developers.google.com/accounts/docs/OAuth2
    /// </summary>
    /// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
    /// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
    /// <param name="userName">A string used to identify a user.</param>
    /// <returns></returns>
    public static YouTubeService AuthenticateOauth(string clientId, string clientSecret, string userName)
            {
    
                string[] scopes = new string[] { YouTubeService.Scope.Youtube,  // view and manage your YouTube account
                                                 YouTubeService.Scope.YoutubeForceSsl,
                                                 YouTubeService.Scope.Youtubepartner,
                                                 YouTubeService.Scope.YoutubepartnerChannelAudit,
                                                 YouTubeService.Scope.YoutubeReadonly,
                                                 YouTubeService.Scope.YoutubeUpload}; 
    
                try
                {
                    // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                                 , scopes
                                                                                                 , userName
                                                                                                 , CancellationToken.None
                                                                                                 , new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
    
                    YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "YouTube Data API Sample",
                    });
                    return service;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException);
                    return null;
    
                }
    
            }    }
    

    用法:

     // Authenticate Oauth2
        String CLIENT_ID = "xxxxx-d0vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com";
        String CLIENT_SECRET = "NDmluNfTgUk6wgmy7cFo64RV";
        var service = Authentication.AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, "test");
    

    Google-dotnet-Samples/ youtube 撕下的代码

    【讨论】:

    • 嗨,Dalm,感谢您宝贵的时间和回复。它帮助了我,但它重定向到 gmail 用户 ID 和密码屏幕,我也无法在我的本地系统上工作,因为它给出了 '400。这是一个错误。错误:redirect_uri_mismatch 应用程序:UplaodTo YouTube' 我认为它只需要 https 而我的本地网址是 Localhost/ 如果对他有任何建议,请做必要的事情。提前致谢。
    • 您发送的位置必须与您在 Google Devloper 控制台中的重定向 uri 字段中输入的完全匹配。它必须是您在机器上导航到的确切文件localhost/youtube.aspx,例如
    • 嗨,Dalm,谢谢你,但如果它没有在浏览器中“登录”,每次都要求登录凭据,我想要一些不会在弹出窗口中询问 gmail 凭据的方法我正在上传视频,我想从服务器端代码本身处理它,因为它在以前的授权版本方法'service.setUserCredentials(googleEmail,googleEmailPassword)中存在;'它将返回服务或它应该返回的令牌之类的东西,如果你有任何sn-p代码让我知道。
    • 客户端登录已关闭,您无法使用登录名和密码。您必须使用 oauth2 并进行身份验证。没有其他方法,没有破解,没有解决方法。 Google 关闭了允许登录名和密码的身份验证端点。您必须使用此方法 Oauth2 是访问 YouTube API 的唯一方法。
    • 嗨,Dalm,感谢您的回复,但是如果我使用上面的代码,每当我尝试在我的应用程序中上传视频时,它都会在单独的弹出窗口中询问用户名和密码,因为用户不会知道电子邮件和密码(这将违反安全规定)所以请告诉我哪里有某种方法可以让我在服务器端验证用户并将视频直接上传到我在 YouTube 中的播放列表。即使我使用了 Oauth2 的客户端 ID 和客户端密钥 ID,它的要求也相同。如果可能,请帮助我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2014-12-30
    • 2012-10-07
    • 2016-08-22
    • 2012-10-27
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多