I don't remember exaclty, what was the problem, but here's the code that
works now. Hope you find there something useful
File tokensFile = new File(TOKENS_FILE);
boolean tokensFileExists = !tokensFile.createNewFile();
Credential googleCredential;
if (tokensFileExists) {
List<String> lines = Files.readAllLines(Paths.get(TOKENS_FILE),
Charset.forName("UTF-8"));
String accessToken = lines.get(0);
String refreshToken = lines.get(1);
googleCredential = getGoogleCredential(accessToken,
refreshToken);
} else {
googleCredential = getGoogleCredential();
overwriteFile(tokensFile, googleCredential.getAccessToken() +
'\n' + googleCredential.getRefreshToken());
}
SpreadsheetService service = new SpreadsheetService("myName");
service.setOAuth2Credentials(googleCredential);
URL spreadsheetsUrl = new URL("
https://spreadsheets.google.com/feeds/spreadsheets/private/full");
SpreadsheetFeed spreadsheetFeed = service.getFeed(spreadsheetsUrl,
SpreadsheetFeed.class);
SpreadsheetEntry spreadsheet = null;
for (SpreadsheetEntry sheet : spreadsheetFeed.getEntries()) {
if (SPREADSHEET_ID.equals(sheet.getId())) {
spreadsheet = sheet;
break;
}
}
if (spreadsheet == null) {
System.out.println("doc not found");
return;
}
WorksheetFeed worksheetFeed =
service.getFeed(spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
WorksheetEntry mainWorksheet = worksheets.get(0);
======================
private static Credential getGoogleCredential(String accessToken, String
refreshToken) {
return new
GoogleCredential.Builder().setClientSecrets(GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET)
.setJsonFactory(new JacksonFactory()).setTransport(new
NetHttpTransport()).build()
.setAccessToken(accessToken).setRefreshToken(refreshToken);
}
2016-03-27 21:53 GMT+03:00 Chris <[email protected]>:
> Hello,
>
> I have exactly the same problem (as described below), ie
> spreadsheet.getWorksheets() returns a 404 while I do have proper access to
> spreadsheets (getTitle() works).
> Hence no access to worksheets.
> I have spent two days on this, any clue would be welcome.
>
>
>
>
>
> Le jeudi 1 mai 2014 14:22:09 UTC+2, Валерий Остапенко a écrit :
>>
>> Thanks Ross, but it doesn't work for me=(
>>
>> SpreadsheetService service = new SpreadsheetService("abc");
>> service.setOAuth2Credentials(
>> googleCredential);
>> URL spreadsheetsUrl = new URL("
>> https://spreadsheets.google.com/feeds/spreadsheets/private/full");
>> SpreadsheetFeed spreadsheetFeed = service.getFeed(spreadsheetsUrl,
>> SpreadsheetFeed.class);
>> SpreadsheetEntry spreadsheet = null;
>> for (SpreadsheetEntry sheet : spreadsheetFeed.getEntries()) {
>> if (SPREADSHEET_ID.equals(sheet.getId())) {
>> spreadsheet = sheet;
>> break;
>> }
>> }
>>
>> spreadsheet.getWorksheets();
>>
>> ResourceNotFoundException.
>>
>>
>> четверг, 1 мая 2014 г., 14:45:25 UTC+4 пользователь Ross Orr написал:
>>>
>>> We've had the same problem with Google Spreadsheets Java API version 3.0
>>> after it worked for over 2 years then suddenly stopped in the last couple
>>> of days.
>>> Tracked the problem down to spreadsheetEntry.getDefaultWorksheet();
>>> doesn’t seem to work in the new Spreadsheets.
>>>
>>> Check out the section in the API doco “Retrieving information about
>>> worksheets” for a workaround. spreadsheet.getWorksheets(); still works ok.
>>> Then iterate through the worksheets.
>>>
>>> Hope this helps.
>>>
>>> On Thursday, 1 May 2014 02:22:04 UTC+10, Валерий Остапенко wrote:
>>>>
>>>> Hello.
>>>>
>>>> I'm using spreadsheet api for java (com.google.gdata.core v.1.47.1 and
>>>> com.google.api-client.google-api-client v.1.18.0-rc).
>>>>
>>>> Starting from yesterday I get 404 error (ResourceNotFoundException)
>>>> while geting the worksheet feed. No code was changed, the application was
>>>> compiled weeks ago.
>>>>
>>>> This causes the error:
>>>> service.getFeed(spreadsheet.getWorksheetFeedUrl(),
>>>> WorksheetFeed.class);
>>>> //com.google.gdata.client.spreadsheet.SpreadsheetService
>>>>
>>>> I've made a test new spreadsheet to demonstrate this:
>>>> spreadsheet.getWorksheetFeedUrlString() = "
>>>> https://spreadsheets.google.com/feeds/worksheets/16NnFQH_k4KTGbAuBGes7nqXlJCWUrUY3-EqepkMBuDw/private/full"
>>>> (the url working in browser).
>>>>
>>>> Any solutions/workarounds are welcome...
>>>>
>>> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Google Spreadsheets API" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/google-spreadsheets-api/XLmubp5Kxkg/unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
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.