Google.GData.Client.GDataRequestException - 身份验证失败,突然老code [英] Google.GData.Client.GDataRequestException - Authentication suddenly fails in old code
问题描述
我突然开始得到下面的异常试图进行身份验证和访问谷歌A S preadsheet当驱动器:
未处理的异常:Google.GData.Client.GDataRequestException:
AUT hentication请求的执行返回意外
结果:404
在Google.GData.Client.Utilities.getAuthException(TokenCollection令牌,
HTT pWebResponse响应)
在Google.GData.Client.Utilities.QueryClientLoginToken(GDataCredentials
GC,S特林服务名,字符串的applicationName,布尔
fUseKeepAlive,IWebProxy PROX yServer,乌里
clientLoginHandler)
在Google.GData.Client.GDataGAuthRequest.QueryAuthToken(GDataCredentials
GC)
在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,日期ifModifiedSince,字符串ETAG,Int64的&安培; CONTENTLENGTH)
在Google.GData.Client.Service.Query(FeedQuery feedQuery)
在Google.GData.Documents.DocumentsService.Query(DocumentsListQuery
feedQuery)
在GoogleLogger.GoogleService.getLastXECLogRows(文件夹名字符串,字符串文件名,行的Int32)
块引用>这是没有任何问题,已经运行了两年code。我首先想到,我可能已经失去了对我的生产系统,但谷歌驱动器加载在我的网页浏览器精细的访问权限。试图在其他几个系统和我得到非常相同。
难道今天他们改变事物的谷歌API吗?这不可能是巧合!
解决方案谷歌已经退休了他们年长的验证API。 OAuth 2.0用户应该使用。
我花了太多的时间来弄清楚如何在这里和那里使用新的API验证与旧的GData API抢夺位和部分信息来自互联网。
我决定用截图分享所有的细节,以节省您的时间。
到<一个href=\"https://console.developers.google.com/project\">https://console.developers.google.com/project
按
创建项目
按钮<醇开始=3>
创建项目。键入一些名称。 <醇开始=4>
到 API和放大器;验证
>证书
,并按创建新客户端ID
按钮。它会自动创建JSON键你 - 忽略
- 按
生成新的P12键
<醇开始=6>
文件下载将自动开始。记住密码,您将需要它来打开刚才下载的文件。
- 下载的文件重命名为Key.p12并将其添加到您的解决方案。请确保您设置
生成操作
和复制到输出目录
相应。<醇开始=8>
安装使用的NuGet谷歌API验证。键入程序包管理器控制台以下
安装封装Google.Apis.Auth
- 复制服务,在第4步生成帐户的电子邮件地址。
<醇开始=10>
授予相应的权限给该用户在谷歌的US preadsheet。
使用以下code查询在S preadsheet。在下面的code取代电子邮件和谷歌小号preadsheet URL地址。
常量字符串ServiceAccountEmail =452351479-q41ce1720qd9l94s8847mhc0toao1fed@developer.gserviceaccount.com;VAR证书=新X509Certificate2(Key.p12,notasecret,X509KeyStorageFlags.Exportable);VAR serviceAccountCredentialInitializer =
新ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
范围=新[] {HTTPS://s$p$padsheets.google.com/feeds}
} .FromCertificate(证书);VAR证书=新ServiceAccountCredential(serviceAccountCredentialInitializer);如果(!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None)。结果)
抛出新的InvalidOperationException异常(访问令牌请求失败。);VAR requestFactory =新GDataRequestFactory(NULL);
requestFactory.CustomHeaders.Add(授权:承载+ credential.Token.AccessToken);VAR的服务=全新S preadsheetsService(NULL){RequestFactory = requestFactory};VAR的查询=新ListQuery(\"https://s$p$padsheets.google.com/feeds/list/0ApZkobM61WIrdGRYshh345523VNsLWc/1/private/full\");
VAR饲料= service.Query(查询);VAR行= feed.Entries
.Cast&LT;一个ListEntry&GT;()
。选择(ARG =&GT;
新
{
Field0 = arg.Elements [0] .value的,
FIELD1 = arg.Elements [1]。价值
})
.ToList();
I'm suddenly starting to get the following exception when attempting to authenticate and access a spreadsheet on Google drive:
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.
Did they change something in the Google API today? This can't be coincidence!
解决方案Google has retired their older authentication API. OAuth 2.0 should be used instead.
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.
Hit
Create Project
button
- Create project. Type in some name.
- Go to
API & Auth
>Credentials
and hitCreate new Client ID
button. It will create JSON key for you automatically - ignore that.
- Hit
Generate new P12 key
- File download will start automatically. Remember the password, you will need it to open the file you just downloaded.
- Rename downloaded file to Key.p12 and add it to your solution. Make sure you set
Build Action
andCopy to Output Directory
accordingly.
Install Google API Auth using Nuget. Type the following in the Package Manager Console
Install-Package Google.Apis.Auth
- Copy service account email address that was generated in Step #4.
Grant appropriate permission to this user in your Google Spreadsheet.
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 - 身份验证失败,突然老code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!