使用预先存在的访问令牌通过ASP.NET创建YouTube服务 [英] Creating a YouTube Service via ASP.NET using a pre-existing Access Token

查看:113
本文介绍了使用预先存在的访问令牌通过ASP.NET创建YouTube服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用一个网站,用户可以将视频上传到共享的YouTube帐户,以便以后访问。经过大量的工作,我已经能够获得一个主动令牌和可行的刷新令牌。

I've been working on a Website for users to upload videos to a shared YouTube account for later access. After much work I've been able to get an Active Token, and viable Refresh Token.

然而,初始化 YouTubeService的代码对象如下所示:

However, the code to initialize the YouTubeService object looks like this:

UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets, 
        // This OAuth 2.0 access scope allows an application to upload files to the
        // authenticated user's YouTube channel, but doesn't allow other types of access.
        new[] { YouTubeService.Scope.YoutubeUpload },
        "user",
        CancellationToken.None
    );
}

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = Assembly.GetExecutingAssembly().GetName().Name,
});

我已经有了一个令牌,我想用我的。我正在使用ASP.NET 3.5版,因此无论如何我都无法进行 async 调用。

I've already got a token, and I want to use mine. I'm using ASP.NET version 3.5, and so I can't do an async call anyways.

有没有办法在没有 async 的情况下创建 YouTubeService 对象,并使用我自己的令牌?有没有办法可以在没有授权经纪人的情况下构建凭据对象?

Is there any way I can create a YouTubeService object without the async call, and using my own token? Is there a way I can build a credential object without the Authorization Broker?

或者,应用程序使用YouTube API V2已经有一段时间了,并且有一个表单可以使用令牌,并针对与API V2中的令牌一起生成的YouTube URI执行了后续操作。有没有办法用V3实现?有没有办法使用Javascript上传视频,可能还有我可以在我的代码中使用的示例?

Alternatively, the application used YouTube API V2 for quite some time, and had a form that took a token, and did a post action against a YouTube URI that was generated alongside the token in API V2. Is there a way I can implement that with V3? Is there a way to use Javascript to upload videos, and possibly an example that I could use in my code?

推荐答案

注意:我最终将我的Framework升级到4.5以访问谷歌库。

NOTE: I ended up upgrading my Framework to 4.5 to access the google libraries.

要以编程方式初始化UserCredential对象,您必须构建一个Flow和TokenResponse。流程需要一个范围(也就是我们正在寻找凭据的权限。

To programatically initialize a UserCredential Object you've got to build a Flow, and TokenResponse. A Flow Requires a Scope (aka the permissions we are seeking for the credentials.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Auth.OAuth2.Flows;

string[] scopes = new string[] {
    YouTubeService.Scope.Youtube,
    YouTubeService.Scope.YoutubeUpload
};

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = new ClientSecrets
    {
        ClientId = XXXXXXXXXX,  <- Put your own values here
        ClientSecret = XXXXXXXXXX  <- Put your own values here
    },
    Scopes = scopes,
    DataStore = new FileDataStore("Store")
});

TokenResponse token = new TokenResponse {
    AccessToken = lblActiveToken.Text,
    RefreshToken = lblRefreshToken.Text
};

UserCredential credential = new UserCredential(flow, Environment.UserName, token);

希望有所帮助。

这篇关于使用预先存在的访问令牌通过ASP.NET创建YouTube服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆