Using the YouTube v3 Data API for .NET, how is it possible to get a refresh token?

我的梦境 提交于 2019-12-02 13:44:33

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!