【发布时间】:2021-11-26 00:30:16
【问题描述】:
作为参考,我关注了这个网站https://qawithexperts.com/article/asp-net/upload-file-to-google-drive-using-google-drive-api-in-aspnet/236,了解如何在 Web 应用程序中将文件上传到 Google 云端硬盘。
这是我获取 DriveService 的代码
public static string[] Scopes = { Google.Apis.Drive.v3.DriveService.Scope.Drive };
public static DriveService GetService()
{
//get Credentials from client_secret.json file
UserCredential credential;
DriveService service = null;
//Root Folder of project
var CSPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
try
{
using (var stream = new FileStream(Path.Combine(CSPath, "client_secret.json"), FileMode.Open, FileAccess.Read))
{
String FolderPath = System.Web.Hosting.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.
service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GoogleDriveMVCUpload",
});
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
System.Diagnostics.Debug.WriteLine(e.Message);
System.Diagnostics.Debug.WriteLine(e.InnerException);
}
return service;
}
从 GoogleWebAuthorizationBroker.AuthorizeAsync 获取凭据时会出现问题。当该行运行时,我将被重定向到 this link(每次运行此代码时,重定向 URI http://127.0.0.1:52829/authorize/ 的端口都会更改)。
我查看了 Google 的错误文档和其他堆栈溢出,解释说我必须在我执行 shown here 的控制台上添加重定向 URI,但它仍然存在 redirect_uri_mismatch 错误。当我只打开错误中给出的重定向 URI(例如:http://127.0.0.1:52829/authorize/)时,我得到以下信息
。但是会抛出异常
Exception thrown: 'System.AggregateException' in mscorlib.dll
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at .........
One or more errors occurred.
Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"", Description:"", Uri:""
at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.<AuthorizeAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__1.MoveNext()
**service** was null.
我找不到太多关于这个特定异常错误的信息:
Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"", Description:"", Uri:"" 但我相信这是因为重定向不匹配 URI 问题。
有关更多信息:Web 应用程序的当前链接是 http://localhost:61506,而我运行上述代码时的链接是 http://localhost:61506/AutoFileTransfer/Index
所以我真的不知道为什么会收到此重定向不匹配 URI 错误,也不知道如何解决
Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"", Description:"", Uri:"" 也有问题。
【问题讨论】:
标签: c# asp.net google-drive-api google-api-dotnet-client