I have OAuth code like:
private val transport =
com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport()
private val json = new com.google.api.client.json.jackson2.JacksonFactory()
private def OAuth2Credentials: GoogleCredential = {
val privateKey: PrivateKey = FileInputStreamResource(new
File(Credentials.AnalyticsOAuth2KeyFilePath)).use { is: InputStream =>
val password: String = Credentials.AnalyticsOAuth2KeyFilePassword
SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
is, password, "privatekey", password)
}
require(null != privateKey, "privateKey is null! Wrong alias for the key?
See comments in this file or README.txt file")
new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(json)
.setServiceAccountId(Credentials.AnalyticsOAuth2AccountId)
.setServiceAccountUser(Credentials.AnalyticsOAuth2AccountUser)
.setServiceAccountPrivateKey(privateKey)
.setServiceAccountScopes(Seq("https://spreadsheets.google.com/feeds",
"https://spreadsheets.google.com/feeds/spreadsheets/private/full",
"https://docs.google.com/feeds",
"https://docs.google.com/a/frugalmechanic.com/spreadsheets/").asJava)
.build()
}
And download code like:
def downloadSpreadsheetToTSV(key: String, gid: Int, googleCredential:
GoogleCredential): String = { /*fm.common.Service.call("Calling GoogleDocs
API", logging = logger, exceptionHandler = exceptionHandler, maxRetries = 3) {*/
//googleCredential.refreshToken()
val service: SpreadsheetService = new
SpreadsheetService("frugalmechanic-tsvdownload-1")
service.setOAuth2Credentials(googleCredential)
//
https://docs.google.com/a/frugalmechanic.com/spreadsheets/d/1HJ-wP0oEPMP9Kz1f17wOabbfXhsqkgk0hHGoBEVmrX8/export?gid=0&exportFormat=tsv
val url = new
URL("https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+key+"&gid="+gid+"&exportFormat=tsv")
//val url = new
URL("https://docs.google.com/a/frugalmechanic.com/spreadsheets/d/"+key+"&gid="+gid+"&exportFormat=tsv")
def makeRequest(url: URL): String = {
val request = service.createEntryRequest(url)
try {
logger.info("Google Docs Export: "+url)
request.execute()
InputStreamResource(request.getResponseStream()).readToString("UTF-8")
} catch {
case e: com.google.gdata.util.RedirectRequiredException =>
makeRequest(new URL(e.getRedirectLocation))
case e: Exception => throw e
} finally {
request.end()
}
}
makeRequest(url)
}
The exception I get is:
13:41:12.879 [main] INFO fm.util.GoogleDocs - Google Docs Export:
https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=1HJ-wP0oEPMP9Kz1f17wOabbfXhsqkgk0hHGoBEVmrX8&gid=0&exportFormat=tsv
13:41:13.244 [main] INFO fm.util.GoogleDocs - Google Docs Export:
https://docs.google.com/spreadsheets/d/1HJ-wP0oEPMP9Kz1f17wOabbfXhsqkgk0hHGoBEVmrX8/export?gid=0&exportFormat=tsv
Exception in thread "main" java.lang.IllegalArgumentException: Trying to
set foreign cookie
at
com.google.gdata.client.http.GoogleGDataRequest$GoogleCookie.<init>(GoogleGDataRequest.java:167)
at
com.google.gdata.client.http.GoogleGDataRequest$GoogleCookieHandler.put(GoogleGDataRequest.java:400)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:727)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1534)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at
sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(HttpURLConnection.java:2952)
at
sun.net.www.protocol.https.HttpsURLConnectionImpl.getHeaderFields(HttpsURLConnectionImpl.java:283)
at
com.google.gdata.client.http.HttpGDataRequest.isOAuthProxyErrorResponse(HttpGDataRequest.java:566)
at
com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:557)
at
com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
at
com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at fm.util.GoogleDocs$.makeRequest$1(GoogleDocs.scala:34)
at fm.util.GoogleDocs$.makeRequest$1(GoogleDocs.scala:32)
at fm.util.GoogleDocs$.downloadSpreadsheetToTSV(GoogleDocs.scala:44)
at
fm.coupon.GoogleDocsCoupons$.downloadCouponDataFromGoogleDocs(GoogleDocsCoupons.scala:113)
at fm.coupon.GoogleDocsCoupons$.update(GoogleDocsCoupons.scala:67)
at fm.coupon.GoogleDocsCoupons$.updateLocalFile(GoogleDocsCoupons.scala:62)
at fm.coupon.GoogleDocsCoupons$.main(GoogleDocsCoupons.scala:59)
at fm.coupon.GoogleDocsCoupons.main(GoogleDocsCoupons.scala)
On Friday, December 11, 2015 at 12:48:35 PM UTC-8, Eric Peters wrote:
>
>
> t's old, is new again - I tried to make this work again and still getting
> the same errors. I'm using an OAuth2 service created using a p12 file.
>
> On Thursday, June 18, 2015 at 8:33:52 AM UTC-7, Eric Koleda wrote:
>>
>> I tried to replicate this using the OAuth2 playground but following the
>> redirect worked without any errors. Are you using OAuth2 to authorize these
>> requests?
>>
>> Best,
>> - Eric
>>
>> On Wednesday, June 17, 2015 at 6:42:29 PM UTC-4, Eric Peters wrote:
>>>
>>> I'm not sure how recently this was changed, but I just noticed it.
>>>
>>> When using the Spreadsheets API, a redirect is now being sent back
>>> (com.google.gdata.util.RedirectRequiredException)
>>> but even if you try to parse that and make a new request - you get another
>>> gdata error relating to java.lang.IllegalArgumentException: Trying to
>>> set foreign cookie (spreadsheets.google.com host doesn't match the
>>> docs.google.com)
>>>
>>>
>>> def makeRequest(url: URL): String = {
>>> val request = service.createEntryRequest(url)
>>>
>>> try {
>>> logger.info("Google Docs Export: "+url)
>>> request.execute()
>>> InputStreamResource(request.getResponseStream()).readToString("UTF-8")
>>> } catch {
>>> case e: com.google.gdata.util.RedirectRequiredException =>
>>> makeRequest(new URL(e.getRedirectLocation))
>>> } finally {
>>> request.end()
>>> }
>>> }
>>>
>>> fm.util.GoogleDocs - Google Docs Export:
>>> https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=1x2KBxXxqFzUWmbbYR9qT1mme0_Ra4-oVf1WWR4As0-Q&gid=0&exportFormat=tsv
>>> fm.util.GoogleDocs - Google Docs Export:
>>> https://docs.google.com/spreadsheets/d/1x2KBxXxqFzUWmbbYR9qT1mme0_Ra4-oVf1WWR4As0-Q/export?gid=0&exportFormat=tsv
>>> fm.util.GoogleDocs - Caught unhandled exception:
>>> java.lang.IllegalArgumentException: Trying to set foreign cookie
>>>
>>>
>>> What happened before I handled the redirect:
>>>
>>> fm.util.GoogleDocs - Calling GoogleDocs API...
>>> fm.util.GoogleDocs - Google Docs Export:
>>> https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=0Ai7IYcrzwmCMdFloak50NGN5cmZlWkVqbXRYMmR2aXc&gid=0&exportFormat=tsv
>>> fm.util.GoogleDocs - Caught unhandled exception:
>>> com.google.gdata.util.RedirectRequiredException: Moved Temporarily
>>> <HTML>
>>> <HEAD>
>>> <TITLE>Moved Temporarily</TITLE>
>>> </HEAD>
>>> <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
>>> <H1>Moved Temporarily</H1>
>>> The document has moved <A
>>> HREF="https://docs.google.com/a/frugalmechanic.com/spreadsheets/d/1HJ-wP0oEPMP9Kz1f17wOabbfXhsqkgk0hHGoBEVmrX8/export?gid=0&exportFormat=tsv">here</A>.
>>> </BODY>
>>> </HTML>
>>>
>>> Any thoughts? this doesn't seem to matter if I do the same request on a
>>> non-Google Apps account vs a Google-Apps account.
>>>
>>> Thanks,
>>>
>>> -Eric
>>>
>>
--
You received this message because you are subscribed to the Google Groups
"Google Spreadsheets API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.