【发布时间】:2018-02-21 09:06:56
【问题描述】:
我正在使用适用于 .NET (Google.Apis.Sheets.v4) 的 API 客户端库开发应用程序,但在身份验证过程中遇到了问题。
SheetsService初始化代码:
private SheetsService __sheetsService;
public GoogleSheetsHelper()
{
// initialize sheets API
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Google Sheets API service.
__sheetsService = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName
});
}
我已经在两台电脑上测试了这段代码,Windows 10 和 Windows 7。Windows 10 没有问题,但在 Windows 7 机器上我收到了这样的异常:
异常文本
System.AggregateException:发生一个或多个错误。 ---> System.Net.HttpListenerException:没有足够的配额可用于 处理这个命令 在 System.Net.HttpListener.SetupV2Config() 在 System.Net.HttpListener.Start() 在 Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener() 在 Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.d__14.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) 在 Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.d__8.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) 在 Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__4.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) 在 Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__1.MoveNext() --- 内部异常堆栈跟踪结束 --- 在 System.Threading.Tasks.Task.ThrowIfExceptional(布尔 includeTaskCanceledExceptions) 在 System.Threading.Tasks.Task
1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() 在 CheesecakeCalculator.GoogleDriveHelper..ctor() 在 CheesecakeCalculator.Form1.Form1_Load(对象发送者,EventArgs e) 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(布尔 fIgnoreVisible) 在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(消息和 m) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ScrollableControl.WndProc(消息和 m) 在 System.Windows.Forms.Form.WmShowWindow(消息和 m) 在 System.Windows.Forms.Form.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) ---> (Inner Exception #0) System.Net.HttpListenerException (0x80004005): 没有足够的配额来处理这个命令 在 System.Net.HttpListener.SetupV2Config() 在 System.Net.HttpListener.Start() 在 Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener() 在 Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.d__14.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) 在 Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.d__8.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) 在 Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__4.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) 在 Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__1.MoveNext()
有谁知道怎么回事? Myabe 需要一些 Windows 配置来解决这个问题,或者其他方式来登录/编写 GoogleSheets? 谢谢。
【问题讨论】:
-
可能是您的请求配额已满,无法使用 google api 发送免费请求。 Google 每天提供有限数量的请求。我假设您使用的是免费服务。
-
是的,我正在使用免费服务,但是测试是同时运行的,并且在 Win10 上运行良好,在 Win7 调用后运行良好
标签: c# .net google-oauth google-sheets-api google-api-dotnet-client