gpt4 book ai didi

c# - 用于 token 的 Google OAuth 数据存储

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:39 32 4
gpt4 key购买 nike

我们的应用程序需要让用户将他们的 Google 日历同步到我们的内部事件模块,但我坚持执行授权并将其存储到我们的数据库中。我正在关注这个样本 Google OAuth仅供引用,但在该示例中据说要实现您自己的使用 EF 的 DataStore,我继续尝试它,但我似乎无法让它工作,我已经被困了几个星期了,任何帮助将不胜感激。谢谢!

我以前有过这样的事情:

public class TokenDataStore : IDataStore
{
private readonly IUnitOfWork _unitOfWork;
private readonly IUserRepository _userRepository;
private readonly IBusinessRepository _businessRepository;

public TokenDataStore(IUnitOfWork unitOfWork, IUserRepository userRepository, IBusinessRepository businessRepository)
{
this._unitOfWork = unitOfWork;
this._userRepository = userRepository;
this._businessRepository = businessRepository;
}

//Todo: Add IdataStore Members

public static string GenerateStoredKey(string key, Type t)
{
return string.Format("{0}-{1}", t.FullName, key);
}
}

更多信息:-MVC 4-.Net 4.5- 英孚 5- 谷歌 API

编辑

好的,所以在过去的几个小时里我取得了一些进展,我终于能够制作自己的 AppFlowMetaData

private readonly IAuthorizationCodeFlow flow;
private readonly GoogleAPI _API;
private readonly IGoogleAPIRepository _googleAPIRepository;
private readonly IUnitOfWork _unitOfWork;

public AppFlowMetadata(GoogleAPI API, IGoogleAPIRepository googleAPIRepository, IUnitOfWork unitOfWork)
{
this._API = API;
this._googleAPIRepository = googleAPIRepository;
this._unitOfWork = unitOfWork;

this.flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = ConfigurationManager.AppSettings["GP_Key"],
ClientSecret = ConfigurationManager.AppSettings["GP_Secret"]
},
Scopes = new[] { CalendarService.Scope.Calendar },
DataStore = new TokenDataStore(_API, _googleAPIRepository, _unitOfWork)
});
}

还有我自己的数据存储类:

private readonly IGoogleAPIRepository _googleAPIRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly GoogleAPI _model;

public TokenDataStore(GoogleAPI model, IGoogleAPIRepository googleAPIRepository, IUnitOfWork unitOfWork)
{
this._googleAPIRepository = googleAPIRepository;
this._unitOfWork = unitOfWork;
this._model = model;
}

现在我在回拨的过程中遇到了问题。因此,当用户使用他们的帐户登录时,在我的数据存储类中,我将 token 作为字符串保存在数据库中,当代码到达我从模型中获取 token 的部分时,传递的数据类型是一个字符串而不是通常的 token 响应。这是我的数据存储的完整代码:

public class TokenDataStore : IDataStore
{
private readonly IGoogleAPIRepository _googleAPIRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly GoogleAPI _model;

public TokenDataStore(GoogleAPI model, IGoogleAPIRepository googleAPIRepository, IUnitOfWork unitOfWork)
{
this._googleAPIRepository = googleAPIRepository;
this._unitOfWork = unitOfWork;
this._model = model;
}

public System.Threading.Tasks.Task StoreAsync<T>(string key, T value)
{
var serialized = NewtonsoftJsonSerializer.Instance.Serialize(value);
if(serialized.Contains("access_token"))
{
_model.TokenString = serialized;

_googleAPIRepository.Save(_model, EntityState.Modified);
_unitOfWork.Commit();
}
return TaskEx.Delay(0);
}

public System.Threading.Tasks.Task DeleteAsync<T>(string key)
{
_model.TokenString = "";

_googleAPIRepository.Save(_model, EntityState.Modified);
_unitOfWork.Commit();

return TaskEx.Delay(0);
}

public Task<T> GetAsync<T>(string key)
{
TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
try
{
tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize<T>(_model.TokenString));
}
catch(Exception ex)
{
tcs.SetException(ex);
}

return tcs.Task;
}

public System.Threading.Tasks.Task ClearAsync()
{
return TaskEx.Delay(0);
}
}

最佳答案

凯文,目前还不清楚是什么不起作用..

可能是:

  1. token 未存储在您的数据库中
  2. 稍后访问用户资源时未进行身份验证

我假设是后者,因此您需要解决两个挑战:

  1. token 过期使数据库无效
  2. 访问用户资源时, token 不会反馈到 googleauthenticator

您需要做的是确保允许离线访问,以便服务器可以在用户不在场或 session 超出范围时访问资源。在这里阅读“离线访问”一章如何做到这一点:Using oAuth for WebApplications

确保在将 token 反馈回身份验证器时验证 token 仍然有效。如果不是,您需要使用刷新 token 来更新身份验证。 Validating token

您需要确保使用您数据库中的 token 调用 googleauthenticator,以确保您的代码具有访问权限。

希望我假设了正确的问题...;)

关于c# - 用于 token 的 Google OAuth 数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20651791/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com