谷歌云端硬盘API - 自定义IDataStore实体框架 [英] Google Drive Api - Custom IDataStore with Entity Framework
问题描述
我实现我的自定义 IDataStore
,这样我可以存储的最终用户令牌的我的数据库,而不是默认实现,这是%APPDATA%以内保存在文件系统
I implemented my custom IDataStore
so that I can store End User Tokens on my database instead of the default implementation, which is saved on FileSystem within %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");
}
问题发生在重定向事件。之后,第一次重新定向正常工作。
The issue happens on the redirect event. After that first redirect it works fine.
我发现的东西是在重定向事件不同。在重定向事件 T
不是一个令牌响应,而是一个字符串。此外,关键是与pfixed $ P $的OAuth _。
I found out that something is different on the redirect event. On the redirect event the T
is not a Token Response, but a string. Also, the key is prefixed with "oauth_".
所以,我认为我应该在重定向返回不同的结果,但我不知道该怎么回。
So I assume that I should return a different result on the redirect, but I have no clue what to return.
我得到的错误是: Google.Apis.Auth.OAuth2.Responses.TokenResponseException:错误:国家是无效的,说明:,乌里:的
The error I get is : Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"State is invalid", Description:"", Uri:""
谷歌来源$ C $ C参考的
<一href=\"https://$c$c.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.DotNet4/Apis/Util/Store/FileDataStore.cs?r=eb702f917c0e18fc960d077af132d0d83bcd6a88\" rel=\"nofollow\">https://$c$c.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.DotNet4/Apis/Util/Store/FileDataStore.cs?r=eb702f917c0e18fc960d077af132d0d83bcd6a88
<一个href=\"https://$c$c.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.Auth/OAuth2/Web/AuthWebUtility.cs?r=eb702f917c0e18fc960d077af132d0d83bcd6a88\" rel=\"nofollow\">https://$c$c.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.Auth/OAuth2/Web/AuthWebUtility.cs?r=eb702f917c0e18fc960d077af132d0d83bcd6a88
感谢您的帮助。
推荐答案
我不知道是什么原因你的心不是工作,但是这是code我用的副本。满级可以在这里找到<一href=\"https://github.com/LindaLawton/Google-Dotnet-Samples/blob/master/Authentication/Diamto.Google.Authentication/Diamto.Google.Authentication/DatabaseDataStore.cs\"相对=nofollow> DatabaseDatastore.cs
I am not exactly sure why yours isnt working but this is a copy of the code i use. The full class can be found here DatabaseDatastore.cs
/// <summary>
/// Returns the stored value for the given key or <c>null</c> if the matching file (<see cref="GenerateStoredKey"/>
/// in <see cref="FolderPath"/> doesn't exist.
/// </summary>
/// <typeparam name="T">The type to retrieve</typeparam>
/// <param name="key">The key to retrieve from the data store</param>
/// <returns>The stored object</returns>
public Task<T> GetAsync<T>(string key)
{
//Key is the user string sent with AuthorizeAsync
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Key MUST have a value");
}
TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
// Note: create a method for opening the connection.
SqlConnection myConnection = new SqlConnection("user id=" + LoginName + ";" +
@"password=" + PassWord + ";server=" + ServerName + ";" +
"Trusted_Connection=yes;" +
"database=" + DatabaseName + "; " +
"connection timeout=30");
myConnection.Open();
// Try and find the Row in the DB.
using (SqlCommand command = new SqlCommand("select RefreshToken from GoogleUser where UserName = @username;", myConnection))
{
command.Parameters.AddWithValue("@username", key);
string RefreshToken = null;
SqlDataReader myReader = command.ExecuteReader();
while (myReader.Read())
{
RefreshToken = myReader["RefreshToken"].ToString();
}
if (RefreshToken == null)
{
// we don't have a record so we request it of the user.
tcs.SetResult(default(T));
}
else
{
try
{
// we have it we use that.
tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize<T>(RefreshToken));
}
catch (Exception ex)
{
tcs.SetException(ex);
}
}
}
return tcs.Task;
}
这篇关于谷歌云端硬盘API - 自定义IDataStore实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!