2

I have suffered through the anti-5-minute-quickstart-experience with the Google Drive API, all in an attempt to upgrade my programmatic use of Google Spreadsheets to be OAuth2 compliant (yes, I know, I was a straggler).

I'm now at the point where I'm simply trying to list the spreadsheets I have on Drive and I get nothing - no data, but also no errors. Drive seems to be happy, but I'm not.

Would very much appreciate any help...

Here's the details:

  • Yes, I do have spreadsheets (several) in my Google Drive. When I am logged in via web browser, I see references to them at: https://spreadsheets.google.com/feeds/spreadsheets/private/full

  • I have created a Google Credential for an installed application. Code for programmatically uploading a Drive Doc (from Google's "quickstart") worked fine.

  • The following code runs just fine, but claims I have no spreadsheets. I'm confused as to why.



    import java.io.File;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;

    import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.api.services.drive.DriveScopes;
    import com.google.gdata.client.spreadsheet.SpreadsheetService;
    import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
    import com.google.gdata.data.spreadsheet.SpreadsheetFeed;

    public class Spread {

      public static void main(String[] args) throws Exception {
        ArrayList scopes = new ArrayList();
        scopes.add(0, DriveScopes.DRIVE);
        scopes.add(1, "https://spreadsheets.google.com/feeds");
        GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(new NetHttpTransport())
            .setJsonFactory(new JacksonFactory())
            .setServiceAccountId(
                "my-service-email-address@developer.gserviceaccount.com")
            .setServiceAccountPrivateKeyFromP12File(new File("/path/to/my/P12/file"))
            .setServiceAccountScopes(scopes).build();

        credential.refreshToken();

        SpreadsheetService service = new SpreadsheetService("tmp");
        service.setOAuth2Credentials(credential);

        // Define the URL to request. This should never change.
        URL SPREADSHEET_FEED_URL = new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

        // Make a request to the API and get all spreadsheets.
        SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
            SpreadsheetFeed.class);
        List spreadsheets = feed.getEntries();

        // Iterate through all of the spreadsheets returned
        int i = 0;
        for (SpreadsheetEntry spreadsheet : spreadsheets) {
          ++i;
          System.out.println(spreadsheet.getId());
        }
        System.out.println("Done " + i);   
      }
    }

The end result of running this code is "Done, 0 sheets found."

Help!

Community
  • 1
  • 1
kvista
  • 5,039
  • 1
  • 23
  • 25
  • 1
    Solved! The key is to share specific spreadsheets with the e-mail address used as the service account ID (see code above). Makes sense in retrospect, but painful to figure out. I would have thought that sharing with the e-mail address of the person who has created a credential was sufficient; that you didn't need the actual credential e-mail address, which one never really uses anyway. Google, I know it's free and I should be thankful, but please improve the simplicity of this if you want more developers to actually use it (which is why I presume money was spent to build it). – kvista May 27 '15 at 19:33
  • Format of `service account ID` https://stackoverflow.com/a/36861121/1589444 – Ramratan Gupta Sep 28 '17 at 13:24

0 Answers0