gpt4 book ai didi

c# - Google.GData.Client.GDataRequestException - 身份验证在旧代码中突然失败

转载 作者:可可西里 更新时间:2023-11-01 03:12:52 24 4
gpt4 key购买 nike

在尝试验证和访问 Google 驱动器上的电子表格时,我突然开始遇到以下异常:

Unhandled Exception: Google.GData.Client.GDataRequestException: Execution of aut hentication request returned unexpected result: 404 at Google.GData.Client.Utilities.getAuthException(TokenCollection tokens, Htt pWebResponse response) at Google.GData.Client.Utilities.QueryClientLoginToken(GDataCredentials gc, S tring serviceName, String applicationName, Boolean fUseKeepAlive, IWebProxy prox yServer, Uri clientLoginHandler) at Google.GData.Client.GDataGAuthRequest.QueryAuthToken(GDataCredentials gc) at Google.GData.Client.GDataGAuthRequest.EnsureCredentials() at Google.GData.Client.GDataRequest.EnsureWebRequest() at Google.GData.Client.GDataGAuthRequest.EnsureWebRequest() at Google.GData.Client.GDataRequest.Execute() at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter) at Google.GData.Client.GDataGAuthRequest.Execute() at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince, String etag, Int64& contentLength) at Google.GData.Client.Service.Query(FeedQuery feedQuery) at Google.GData.Documents.DocumentsService.Query(DocumentsListQuery feedQuery ) at GoogleLogger.GoogleService.getLastXECLogRows(String folderName, String fileName, Int32 rows)

这是运行了两年没有任何问题的代码。我首先想到我可能失去了对我的生产系统的访问权限,但谷歌驱动器在我的网络浏览器中加载正常。在其他几个系统上试过,结果完全一样。

他们今天是否更改了 Google API 中的某些内容?这不是巧合!

最佳答案

Google 已停用其旧的身份验证 API。应改用 OAuth 2.0。

我花了太多时间弄清楚如何将较新的 Auth API 与较旧的 GData API 结合使用,从互联网上到处获取信息。我决定通过屏幕截图分享所有详细信息,以节省您的时间。

  1. 转到 https://console.developers.google.com/project

  2. 点击创建项目按钮

enter image description here

  1. 创建项目。输入一些名字。

enter image description here

  1. 转到 API & Auth > Credentials 并点击 Create new Client ID 按钮。它会自动为您创建 JSON key - 忽略它。

enter image description here

  1. 点击生成新的 P12 key

enter image description here

  1. 文件下载将自动开始。记住密码,您将需要它来打开刚刚下载的文件。

enter image description here

  1. 将下载的文件重命名为 Key.p12 并将其添加到您的解决方案中。确保相应地设置 Build ActionCopy to Output Directory

enter image description here

  1. 使用 Nuget 安装 Google API Auth。在包管理器控制台中键入以下内容

    Install-Package Google.Apis.Auth

enter image description here

  1. 复制在第 4 步中生成的服务帐户电子邮件地址。

enter image description here

  1. 在您的 Google 电子表格中向该用户授予适当的权限。

  2. 使用以下代码查询电子表格。替换下面代码中的电子邮件和 Google 电子表格 URL 地址。

    const string ServiceAccountEmail = "452351479-q41ce1720qd9l94s8847mhc0toao1fed@developer.gserviceaccount.com";

    var certificate = new X509Certificate2("Key.p12", "notasecret", X509KeyStorageFlags.Exportable);

    var serviceAccountCredentialInitializer =
    new ServiceAccountCredential.Initializer(ServiceAccountEmail)
    {
    Scopes = new[] { "https://spreadsheets.google.com/feeds" }
    }.FromCertificate(certificate);

    var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);

    if (!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
    throw new InvalidOperationException("Access token request failed.");

    var requestFactory = new GDataRequestFactory(null);
    requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken);

    var service = new SpreadsheetsService(null) { RequestFactory = requestFactory };

    var query = new ListQuery("https://spreadsheets.google.com/feeds/list/0ApZkobM61WIrdGRYshh345523VNsLWc/1/private/full");
    var feed = service.Query(query);

    var rows = feed.Entries
    .Cast<ListEntry>()
    .Select(arg =>
    new
    {
    Field0 = arg.Elements[0].Value,
    Field1 = arg.Elements[1].Value
    })
    .ToList();

关于c# - Google.GData.Client.GDataRequestException - 身份验证在旧代码中突然失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30469058/

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