【发布时间】:2021-07-30 22:13:35
【问题描述】:
我遇到了 google drive API 的问题,它正在我的本地计算机上运行,但是一旦我将项目部署到服务器,google drive API 服务就会停止工作,
它引发了 oauth2 异常,我想我需要在 google drive api 控制台上做一些事情,但我不知道它是什么或从哪里开始
我当前使用的凭据是 OAuth 2.0 客户端 ID 和客户端密钥,键入 Desktop。
感谢您的帮助
这是我得到的例外。
An unhandled exception occurred while processing the request.
AggregateException: One or more errors occurred. (Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=813040219774-atf2l4751d1tkjeegoeb7d4lituel9ev.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A42257%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive" for authorization. See inner exception for details.)
System.Threading.Tasks.Task<TResult>.GetResultCore(bool waitCompletionNotification)
NotSupportedException: Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=813040219774-atf2l4751d1tkjeegoeb7d4lituel9ev.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A42257%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive" for authorization. See inner exception for details.
Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.ReceiveCodeAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken)
Stack Query Cookies Headers Routing
AggregateException: One or more errors occurred. (Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=813040219774-atf2l4751d1tkjeegoeb7d4lituel9ev.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A42257%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive" for authorization. See inner exception for details.)
System.Threading.Tasks.Task<TResult>.GetResultCore(bool waitCompletionNotification)
ElseForty.FileServices.Files+GoogleDrive.GetService() in Files.cs
ElseForty.FileServices.Files+GoogleDrive.UploadFileAsync(IFormFile file) in Files.cs
ElseForty.Controllers.BugReportController.Send(User_BugReportViewModel model) in BugReportController.cs
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask<IActionResult> actionResultValueTask)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Show raw exception details
NotSupportedException: Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=813040219774-atf2l4751d1tkjeegoeb7d4lituel9ev.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A42257%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive" for authorization. See inner exception for details.
Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.ReceiveCodeAsync(AuthorizationCodeRequestUrl url, CancellationToken taskCancellationToken)
Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.AuthorizeAsync(string userId, CancellationToken taskCancellationToken)
Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(Initializer initializer, IEnumerable<string> scopes, string user, CancellationToken taskCancellationToken, IDataStore dataStore, ICodeReceiver codeReceiver)
Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(ClientSecrets clientSecrets, IEnumerable<string> scopes, string user, CancellationToken taskCancellationToken, IDataStore dataStore, ICodeReceiver codeReceiver)
Show raw exception details
更新
我正在为我的网络应用程序使用 google drive api 供访客上传图片,
public static DriveService GetService()
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "elsefortydisk",
});
}
public static async Task<string> UploadFileAsync(IFormFile file)
{
var fileName = GetUniqueFileName(file.FileName);
var service = GetService();
var fileMetadata = new Google.Apis.Drive.v3.Data.File();
fileMetadata.Name = Path.GetFileName(fileName);
fileMetadata.MimeType = file.ContentType;
fileMetadata.Parents = new List<string>() { "14kECd48VL6xhS9ArQL3Lh7oLT6npAVg-" };
FilesResource.CreateMediaUpload request;
using (var stream = file.OpenReadStream())
{
request = service.Files.Create(fileMetadata, stream, file.ContentType);
request.Fields = "id";
await request.UploadAsync();
}
var responce = request.ResponseBody;
var pemission = new Permission();
pemission.Type = "anyone";
pemission.Role = "reader";
try
{
service.Permissions.Create(pemission, responce.Id).Execute();
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
return responce.Id;
}
【问题讨论】:
-
请编辑您的问题并包含minimal reproducible example,而不会看到您的代码很难提供帮助
-
嗨@DaImTo 我在上面包含了一些代码,希望这可以帮助你更好地理解我想要做的事情,thanx
-
我更新了我的答案,以更好地解释为什么您不能将 GoogleWebAuthorizationBroker.AuthorizeAsync 用于 Web 应用程序,并向您展示您应该如何编码您的应用程序。
标签: google-api google-drive-api asp.net-core-mvc google-oauth google-api-dotnet-client