【发布时间】:2020-12-17 02:45:01
【问题描述】:
我实现了我的自定义 IDataStore,以便我可以将 最终用户令牌 存储在我的 数据库 上,而不是保存在 FileSystem 在 %AppData% 内。
public class GoogleIDataStore : IDataStore
{
...
public Task<T> GetAsync<T>(string key)
{
TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
var user = repository.GetUser(key.Replace("oauth_", ""));
var credentials = repository.GetCredentials(user.UserId);
if (key.StartsWith("oauth") || credentials == null)
{
tcs.SetResult(default(T));
}
else
{
var JsonData = Newtonsoft.Json.JsonConvert.SerializeObject(Map(credentials));
tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize<T>(JsonData));
}
return tcs.Task;
}
}
控制器
public async Task<ActionResult> AuthorizeDrive(CancellationToken cancellationToken)
{
var result = await new AuthorizationCodeMvcApp(this, new GoogleAppFlowMetadata()).
AuthorizeAsync(cancellationToken);
if (result.Credential == null)
return new RedirectResult(result.RedirectUri);
var driveService = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = result.Credential,
ApplicationName = "My app"
});
//Example how to access drive files
var listReq = driveService.Files.List();
listReq.Fields = "items/title,items/id,items/createdDate,items/downloadUrl,items/exportLinks";
var list = listReq.Execute();
return RedirectToAction("Index", "Home");
}
问题发生在重定向事件上。在第一次重定向之后它工作正常。
我发现重定向事件有所不同。在重定向事件中,T 不是令牌响应,而是一个字符串。此外,密钥以“oauth_”为前缀。
所以我假设我应该在重定向上返回不同的结果,但我不知道要返回什么。
我得到的错误是:Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"State is invalid", Description:"", Uri:""强>
感谢您的帮助
【问题讨论】:
标签: c# asp.net-mvc oauth-2.0 google-api google-oauth