【发布时间】:2020-05-27 09:00:20
【问题描述】:
我正在用 ASP.NET MVC (C#) 开发一个 Web 应用程序,它与 Google Drive API 配合使用。我已经解决了使用 OAuth 2.0 进行身份验证的整个问题,并且我正在完美地使用 API。问题是,在登录并授予我的应用程序工作权限后,如果我从另一台计算机访问我网站的 URL,它不会请求授权,而是使用最初已经通过身份验证的帐户。
换句话说,我需要能够访问我的网站并使用不同的用户帐户使用 Google 云端硬盘。每个人都在其中进行身份验证并使用他们的 Google Drive。
代码如下:
public static Google.Apis.Drive.v3.DriveService GetService_v3()
{
UserCredential credential;
using (var stream = new FileStream(HostingEnvironment.MapPath("~/client_secret.json"), FileMode.Open, FileAccess.Read))
{
String FolderPath = HostingEnvironment.MapPath("~");
String FilePath = Path.Combine(FolderPath, "DriveServiceCredentials.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(FilePath, true)).Result;
}
//Create Drive API service.
Google.Apis.Drive.v3.DriveService service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveRestAPI-v3",
});
return service;
}
【问题讨论】:
标签: c# google-api google-drive-api google-oauth google-api-dotnet-client