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

查看:113
本文介绍了使用适用于.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,并将Controller Action设置为接收Google Developer Console中指定的回调)和Token的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天全站免登陆