使用适用于.NET的YouTube v3 Data API,如何获得刷新令牌?

我需要能够使用刷新令牌,以便在访问令牌过期后重新validation令牌。 如何使用C#v3 API执行此操作? 我查看了UserCredential类和AuthorizationCodeFlow类,没有任何内容向我跳出来。

我正在使用以下代码对其进行身份validation。

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

如果有人可以提出任何建议,我将非常感激。 谢谢。

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

这是我将access_type设置为“离线”的唯一方法。 如果.NET API这样做,我仍然很想知道如何。

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

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

我希望这有助于其他人。