Google.GData.Client.GDataRequestException - 旧代码中的身份验证突然失败 [英] Google.GData.Client.GDataRequestException - Authentication suddenly fails in old code

查看:13
本文介绍了Google.GData.Client.GDataRequestException - 旧代码中的身份验证突然失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试对 Google 云端硬盘上的电子表格进行身份验证和访问时,我突然开始遇到以下异常:

I'm suddenly starting to get the following exception when attempting to authenticate and access a spreadsheet on Google drive:

未处理的异常:Google.GData.Client.GDataRequestException:执行身份验证请求返回意外结果:404在 Google.GData.Client.Utilities.getAuthException(TokenCollection 令牌,Htt pWebResponse 响应)在 Google.GData.Client.Utilities.QueryClientLoginToken(GDataCredentialsgc, String serviceName, String applicationName, BooleanfUseKeepAlive、IWebProxy 代理服务器、UriclientLoginHandler)在 Google.GData.Client.GDataGAuthRequest.QueryAuthToken(GDataCredentialsGC)在 Google.GData.Client.GDataGAuthRequest.EnsureCredentials()在 Google.GData.Client.GDataRequest.EnsureWebRequest()在 Google.GData.Client.GDataGAuthRequest.EnsureWebRequest()在 Google.GData.Client.GDataRequest.Execute()在 Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)在 Google.GData.Client.GDataGAuthRequest.Execute()在 Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince, String etag, Int64& contentLength)在 Google.GData.Client.Service.Query(FeedQuery feedQuery)在 Google.GData.Documents.DocumentsService.Query(DocumentsListQuery提要查询)在 GoogleLogger.GoogleService.getLastXECLogRows(String folderName, String fileName, Int32 rows)

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)

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

This is in code that has been running for two years without any problems. I first thought that I may have lost access permissions on my production system but Google drive loads fine in my web browser. Tried it on several other systems and am getting the very same.

他们今天对 Google API 有什么改变吗?这不可能是巧合!

Did they change something in the Google API today? This can't be coincidence!

推荐答案

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

Google has retired their older authentication API. OAuth 2.0 should be used instead.

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

I spent too much time to figure out how to use newer Auth API with older GData API grabbing bits and pieces of information here and there from the Internet. I decided to share all the the details with screenshots to save your time.

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

点击创建项目按钮

  1. 创建项目.输入一些名称.

  1. 转到API &Auth > Credentials 并点击Create new Client ID 按钮.它会自动为您创建 JSON 密钥 - 忽略它.
  1. Go to API & Auth > Credentials and hit Create new Client ID button. It will create JSON key for you automatically - ignore that.

  1. 点击生成新的P12密钥

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

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

  1. 使用 Nuget 安装 Google API 身份验证.在包管理器控制台中输入以下内容

  1. Install Google API Auth using Nuget. Type the following in the Package Manager Console

Install-Package Google.Apis.Auth

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

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

  1. Grant appropriate permission to this user in your Google Spreadsheet.

使用以下代码查询电子表格.替换以下代码中的电子邮件和 Google 电子表格网址.

Use the following code to query the spreadsheet. Replace email and Google spreadsheet URL address in the code below.

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();

这篇关于Google.GData.Client.GDataRequestException - 旧代码中的身份验证突然失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆