1

I have a Java application that uses the Google Contact API to sync contacts with a a local PDA application. It was working fine on my old machine, but after copying the source and everything (including client_secrets.json) to a new laptop, I now get the following error calling com.google.gdata.client.Service.getFeed():

com.google.gdata.util.ServiceForbiddenException: Cannot request contacts belonging to another user

I am passing in the same user ID and password as before, so I'm not sure why it says "another user".

Here's my relevant code:

    // Initialize Contacts service      
    service = new ContactsService(cfg.gAppName);

    // Authorize the service

    Credential credential = authorize(CONTACTS_SCOPE, "contacts");
    if (credential != null && (credential.getExpiresInSeconds() == null || credential.getExpiresInSeconds() < 5)) {
        credential.refreshToken();
    }

    String token = credential.getAccessToken();
    service.setHeader("Authorization", "Bearer " + token);              

    // Request the feed
    contFeedUrl = new URL(BASE_CONT_URL + CONTACTS + cfg.gUser + "/full?max-results=999999");
    if (bFeed) {
        resultFeed = service.getFeed(contFeedUrl, ContactFeed.class);
    }

And this is my authorize() method:

    protected Credential authorize(String scope, String subDir) throws Exception {

    File dataStoreDir = new File(System.getProperty("user.home"), ".store/gsync/" + cfg.dataStore + "/" + subDir);

    // initialize the transport
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    // initialize the data store factory
    dataStoreFactory = new FileDataStoreFactory(dataStoreDir);

    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(SyncMgr.class.getResourceAsStream("/client_secrets.json")));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.out.println(
                "Enter Client ID and Secret from https://code.google.com/apis/console/?api=calendar "
                + "into /client_secrets.json");
        System.exit(1);
    }
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, JSON_FACTORY, clientSecrets,
            Collections.singleton(scope)).setDataStoreFactory(dataStoreFactory).build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(cfg.gUser);
}

Any ideas how I can get it working on the new machine?

Eric S
  • 133
  • 1
  • 9
  • Check this SO question [30511117](http://stackoverflow.duapp.com/questions/30511117/using-java-and-oauth-2-0-to-connect-to-google-contacts) and [20494138](http://stackoverflow.com/questions/20494138/google-contacts-get-contacts-of-another-user-using-service-account) if it can help you. – KENdi Sep 24 '16 at 14:03
  • I checked those two questions and did not really help. It seems like the gist of the answers in those two other questions is: make sure you're doing OAuth2 correctly. I thought I was because, as mentioned before, this worked fine on another machine. It's just when I moved the code to a new machine, it stopped working. I'm thinking that maybe the problem is that I haven't done the user login and consent yet on the new machine. But I'm not sure how to force that to happen. Anyone have an idea how? – Eric S Sep 27 '16 at 21:52
  • I figured it out! I had copied over all of the source code, jars, etc. from the old machine to new. They were all under the same directory. But I didn't copy over the data store directory. Which was under c:\users\(user)\.store. Once I did that, all was working again. – Eric S Sep 28 '16 at 15:48

0 Answers0