使用适用于 .NET 的 YouTube v3 数据 API,如何获得刷新令牌? [英] Using the YouTube v3 Data API for .NET, how is it possible to get a refresh token?

查看:17
本文介绍了使用适用于 .NET 的 YouTube v3 数据 API,如何获得刷新令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够使用刷新令牌才能在访问令牌过期后重新验证令牌.如何使用 C# v3 API 执行此操作?我查看了 UserCredential 类和 AuthorizationCodeFlow 类,但没有任何反应.

I need to be able to use a refresh token to be able to re-authenticate a token after the access token has expired. How can I do this using the C# v3 API? I've looked at the UserCredential class and AuthorizationCodeFlow class and nothing is jumping out at me.

我最初使用以下代码对其进行身份验证.

I'm using the following code to authenticate it originally.

var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
            AuthorizeAsync(CancellationToken.None);
if (result.Credential != null)
{
    var service = new YouTubeService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName = "YouTube Upload Tool"
                });
}

这是我的 AppFlowMetadata 类.

public class AppFlowMetadata : FlowMetadata
{
    private static readonly IAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {

            ClientSecrets = new ClientSecrets
            {
                ClientId = "ID",
                ClientSecret = "SECRET",
            },
            Scopes = new[] { YouTubeService.Scope.YoutubeUpload },
            DataStore = new EFDataStore(-1) // A data store I implemented using Entity Framework 6.
        });

    public override string GetUserId(Controller controller)
    {
        return "test";

    }

    public override IAuthorizationCodeFlow Flow
    {
        get { return flow; }
    }
}

如果有人可以提出任何建议,我将不胜感激.谢谢.

If anyone can suggest anything, I would greatly appreciate it. Thank you.

推荐答案

虽然这不是答案,但我是这样解决的.我必须创建 GET 授权请求(将您的用户重定向到您返回的 url 并设置您的控制器操作以接收在您的 Google Developer Console 中指定的回调)和令牌的 PUT 请求(然后我使用 EF6 存储)手动.我使用 System.Net.Http.HttpClient 发出这些请求,这非常简单.有关我需要获取的所有详细信息,请参阅此链接这个工作.

While this is not an answer, this is how I got around it. I had to create the GET request for authorisation (redirect your user to the url you get back and set your Controller Action to receive the callback specified in your Google Developer Console) and the PUT request for the Token (which I then stored using EF6) manually. I used System.Net.Http.HttpClient to make these requests, which was quite straight forward. See this link for all the details I needed to get this working.

这是我将 access_type 设置为离线"的唯一方法.如果 .NET API 做到了这一点,我仍然很想知道如何做到这一点.

It was the only way I could set the access_type to "offline". If the .NET API does this, I'm still curious to find out how.

存储令牌数据后,我现在使用 API 在需要时验证和刷新令牌.我实际上是在服务器端控制台应用程序而不是 MVC 应用程序中执行此操作(因此是 EF 令牌持久性).

With the token data stored, I now use the API to validate and refresh the token when I need to. I actually did this in a server side console application rather than a MVC app (hence the EF token persistence).

UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets
    {
        ClientId = "ID",
        ClientSecret = "Secret"
    },
    new[] { YouTubeService.Scope.YoutubeUpload },
    "12345",
    CancellationToken.None, 
    new EFDataStore(-1) // My own implementation of IDataStore
            );

    // This bit checks if the token is out of date, 
    // and refreshes the access token using the refresh token.
    if(credential.Token.IsExpired(SystemClock.Default))
    {
        if (!await credential.RefreshTokenAsync(CancellationToken.None))
        {
            Console.WriteLine("No valid refresh token.");
        }
    }            

    var service = new YouTubeService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "MY App"
    });

我希望这能帮助其他人.

I hope this helps others.

这篇关于使用适用于 .NET 的 YouTube v3 数据 API,如何获得刷新令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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