gpt4 book ai didi

c# - MVC 中的 GoogleWebAuthorizationBroker For Google Drive Access

转载 作者:太空狗 更新时间:2023-10-29 21:17:34 30 4
gpt4 key购买 nike

我无法尝试从 MVC 应用程序访问特定的 Google 云端硬盘帐户。我所需要的只是让 MVC 网络应用程序访问我的谷歌驱动器扫描一些文件并根据谷歌驱动器的内容更改数据库。问题是在 IIS 中运行时,驱动器无法通过身份验证,因为 GoogleWebAuthorizationBroker 尝试打开浏览器(如果它是 Windows 应用程序)但似乎无法通过 IIS 执行此操作,即使它可以在服务器端执行此操作。

理想情况下,我根本不需要对这个应用程序进行身份验证,但如果它确实通过了身份验证,那么我该如何让它在 IIS 中运行?

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "MY_ID",
ClientSecret = "My_Secret"
},
new[] { DriveService.Scope.Drive },
"user",
CancellationToken.None, dataStore: new FileDataStore(Server.MapPath("~/app_data/googledata"))).Result;

最佳答案

我让它工作了,能够使该网站能够使用我的帐户访问 Google 云端硬盘,而无需要求用户登录或授权。

首先,点击此链接让 Google API 与 MVC 一起工作:

https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web_applications

HomeController 中的示例代码有问题

 public async Task IndexAsync(CancellationToken cancellationToken)

应该是:

 public async Task<ActionResult> IndexAsync(CancellationToken cancellationToken)

之后,我创建了一个 MemoryDataStore(请参阅最后的代码),它是对此处发布的 MemoryDataStore 的稍微修改:

http://conficient.wordpress.com/2014/06/18/using-google-drive-api-with-c-part-2/

一旦你这样做,捕获你正在使用的帐户的刷新 token ,并在验证时用这个商店替换商店:

    private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = clientID,
ClientSecret = clientSecret
},
Scopes = new[] { DriveService.Scope.Drive },
//DataStore = new FileDataStore("Drive.Api.Auth.Store")
DataStore = new GDriveMemoryDataStore(commonUser, refreshToken)
});

此处 commonUser 是您选择的预定义用户 ID。请确保修改 GetUserID() 方法以返回相同的 commonUser:

 public override string GetUserId(Controller controller)
{
return commonUser;
}

完成此操作后,Google 云端硬盘将停止要求用户登录和授权该应用。

这是我的 MemoryDataStore 代码:

 /// <summary>
/// Handles internal token storage, bypassing filesystem
/// </summary>
internal class GDriveMemoryDataStore : IDataStore
{
private Dictionary<string, TokenResponse> _store;
private Dictionary<string, string> _stringStore;

//private key password: notasecret

public GDriveMemoryDataStore()
{
_store = new Dictionary<string, TokenResponse>();
_stringStore = new Dictionary<string, string>();
}

public GDriveMemoryDataStore(string key, string refreshToken)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
if (string.IsNullOrEmpty(refreshToken))
throw new ArgumentNullException("refreshToken");

_store = new Dictionary<string, TokenResponse>();

// add new entry
StoreAsync<TokenResponse>(key,
new TokenResponse() { RefreshToken = refreshToken, TokenType = "Bearer" }).Wait();
}

/// <summary>
/// Remove all items
/// </summary>
/// <returns></returns>
public async Task ClearAsync()
{
await Task.Run(() =>
{
_store.Clear();
_stringStore.Clear();
});
}

/// <summary>
/// Remove single entry
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public async Task DeleteAsync<T>(string key)
{
await Task.Run(() =>
{
// check type
AssertCorrectType<T>();

if (typeof(T) == typeof(string))
{
if (_stringStore.ContainsKey(key))
_stringStore.Remove(key);
}
else if (_store.ContainsKey(key))
{
_store.Remove(key);
}
});
}

/// <summary>
/// Obtain object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public async Task<T> GetAsync<T>(string key)
{
// check type
AssertCorrectType<T>();

if (typeof(T) == typeof(string))
{
if (_stringStore.ContainsKey(key))
return await Task.Run(() => { return (T)(object)_stringStore[key]; });
}
else if (_store.ContainsKey(key))
{
return await Task.Run(() => { return (T)(object)_store[key]; });
}
// key not found
return default(T);
}

/// <summary>
/// Add/update value for key/value
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public Task StoreAsync<T>(string key, T value)
{
return Task.Run(() =>
{
if (typeof(T) == typeof(string))
{
if (_stringStore.ContainsKey(key))
_stringStore[key] = (string)(object)value;
else
_stringStore.Add(key, (string)(object)value);
} else
{
if (_store.ContainsKey(key))
_store[key] = (TokenResponse)(object)value;
else
_store.Add(key, (TokenResponse)(object)value);
}
});
}

/// <summary>
/// Validate we can store this type
/// </summary>
/// <typeparam name="T"></typeparam>
private void AssertCorrectType<T>()
{
if (typeof(T) != typeof(TokenResponse) && typeof(T) != typeof(string))
throw new NotImplementedException(typeof(T).ToString());
}
}

关于c# - MVC 中的 GoogleWebAuthorizationBroker For Google Drive Access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22598348/

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