You get redirected because accessing your spreadsheet requires that you authenticate first. Google Sheets uses the old gdata API, but requires authentication using OAuth 2.0. Therefore, you will need to import both the gdata libraries and the Google APIs, as shown below:
<dependencies> <dependency> <groupId>com.google.gdata</groupId> <artifactId>core</artifactId> <version>1.47.1</version> </dependency> <dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client-java6</artifactId> <version>1.20.0</version> </dependency> </dependencies>
The code below shows how you can authenticate with Google using OAuth. You need to follow the instructions to create a service account and first download the P12 key. After creating the service account, copy the email address to CLIENT_ID below, add your P12 file to your classes path and chante P12FILE to point to your P12 file.
I managed to get this to work with the following SPREADSHEET_FEED_URL " https://spreadsheets.google.com/feeds/worksheets/:worksheetId/private/basic " where ": worksheetId" is your worksheet sheet. This is a little different than the one you used.
Be sure to ensure that your service account has permission to read or write to the spreadsheet by first sharing it with the email address of the service account.
public class GoogleSheetsApiTest { // Generate a service account and P12 key: // https://developers.google.com/identity/protocols/OAuth2ServiceAccount private final String CLIENT_ID = "<your service account email address>"; // Add requested scopes. private final List<String> SCOPES = Arrays .asList("https://spreadsheets.google.com/feeds"); // The name of the p12 file you created when obtaining the service account private final String P12FILE = "/<your p12 file name>.p12"; @Test public void testConnectToSpreadSheet() throws GeneralSecurityException, IOException, ServiceException, URISyntaxException { SpreadsheetService service = new SpreadsheetService( "google-spreadsheet"); GoogleCredential credential = getCredentials(); service.setOAuth2Credentials(credential); URL SPREADSHEET_FEED_URL = new URL( "https://spreadsheets.google.com/feeds/worksheets/1UXoGD2gowxZ2TY3gooI9y7rwWTPBOA0dnkeNYwUqQRA/private/basic"); SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class); List<SpreadsheetEntry> spreadsheets = feed.getEntries(); if (spreadsheets.size() == 0) { // // TODO: There were no spreadsheets, act accordingly. } // SpreadsheetEntry spreadsheet = spreadsheets.get(0); System.out.println(spreadsheet.getTitle().getPlainText()); } private GoogleCredential getCredentials() throws GeneralSecurityException, IOException, URISyntaxException { JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); HttpTransport httpTransport = GoogleNetHttpTransport .newTrustedTransport(); URL fileUrl = this.getClass().getResource(P12FILE); GoogleCredential credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(JSON_FACTORY) .setServiceAccountId(CLIENT_ID) .setServiceAccountPrivateKeyFromP12File( new File(fileUrl.toURI())) .setServiceAccountScopes(SCOPES).build(); return credential; } }
Brian chapman
source share