我需要从我的web应用程序访问Google电子表格。这是一个会员应用程序,将会员信息写入谷歌电子表格。当我在本地运行代码时,下面的代码可以工作。
SpreadsheetsService service = new SpreadsheetsService(spreadsheetName);
service.setUserCredentials(USERNAME, PASSWORD);
但是,当我在服务器上运行web应用程序时,身份验证失败。
Google.GData.Client.InvalidCredentialsException: Invalid credentials
at Google.GData.Client.Utilities.QueryClientLoginToken(GDataCredentials gc, String serviceName, String applicationName, Boolean fUseKeepAlive, IWebProxy proxyServer, 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)
我还收到了来自谷歌的这封电子邮件:
Hi xxxxxx,
Someone recently used your password to try to sign in to your Google Account
xxxx@gmail.com. This person was using an application such as an email client
or mobile device.
We prevented the sign-in attempt in case this was a hijacker trying to access
your account. Please review the details of the sign-in attempt:
我知道凭据是正确的,因为它们在本地测试时有效。
我看到很多关于身份验证的OAuth。问题是我不想使用用户的身份验证。应该只允许我的网络应用程序使用我提供的凭据写入电子表格。
我转到下面的URL,点击按钮,一切都正常了。真奇怪,需要这个步骤。
https://accounts.google.com/b/0/DisplayUnlockCaptcha