我需要能够使用刷新令牌,以便能够在访问令牌过期后重新验证令牌。我如何使用c# v3 API做到这一点?我已经查看了UserCredential类和AuthorizationCodeFlow类,没有任何东西跳出来。
我使用下面的代码来验证它的原始身份。
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在需要时验证和刷新令牌。我实际上是在一个服务器端控制台应用程序而不是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"
});