Reputation: 21
As Google has deprecated use of OAuth1, I am trying to migrate my application to OAuth2. I am using Service Account and trying to read a Google spreadsheet. I am able to get the access token but still not able to get the spreadsheets associated with my account.Below is the code I am using,please let me know where am I making a mistake. :
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";
URL SPREADSHEET_FEED_URL = new URL(SPREADSHEET_URL);
String[] SCOPESArray = { "https://spreadsheets.google.com/feeds" };
final List SCOPES = Arrays.asList(SCOPESArray);
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("28031770151-826a7iuekrsmr0gf4kc####[email protected]")
.setServiceAccountScopes(SCOPES).setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
.build();
System.out.println(credential.getAccessToken());
credential.refreshToken();
credential.getRefreshToken();
SpreadsheetService service = new SpreadsheetService("inlaid-chassis-9640");
service.setOAuth2Credentials(credential);
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
// spreadsheets comes out be a blank array
Upvotes: 1
Views: 368
Reputation: 21
Solved! For this we need to share our spreadsheet with the service account id/email Id(refer the code above) generated while creating the project on https://console.developers.google.com.
Upvotes: 1