Using the YouTube v3 Data API for .NET, how is it

2019-03-04 17:30发布

问题:

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"
                });
}

And this is my AppFlowMetadata class.

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.

回答1:

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.