Java Google Contacts API Access Service Account Authentication

后端 未结 1 980
南旧
南旧 2020-12-21 06:20

I\'m trying to access Googles Contacts API but my attempt failed already on getting authorized. From other (web) languages i\'m used to the APIConsole and the public API-key

相关标签:
1条回答
  • 2020-12-21 06:33

    Without calling setServiceAccountUser() my code just worked perfectly fine. But you just will have an impersonated account (service_account_mail) not your personal contacts.

    Another possible source for a "401 Unauthorized" exception is leaving the credential.refreshToken() away. The call is necessary to write the access-code into the reference.

    Below the finished class:

    import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
    import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
    import com.google.api.client.http.HttpTransport;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.gdata.client.contacts.ContactsService;
    
    import java.io.File;
    import java.io.IOException;
    import java.security.GeneralSecurityException;
    import java.util.Collections;
    
    public class Connector {
    
      private static ContactsService contactService = null;
      private static HttpTransport httpTransport;
    
      private static final String APPLICATION_NAME = "Your-App/1.0";
      private static final String SERVICE_ACCOUNT_EMAIL = "xy@developer.gserviceaccount.com";
    
      private Connector() {
        // explicit private no-args constructor
      }
    
      public static ContactsService getInstance() {
        if (contactService == null) {
          try {
            contactService = connect();
    
          } catch (GeneralSecurityException | IOException e) {
            e.printStackTrace();
          }
        }
    
        return contactService;
      }
    
      private static ContactsService connect() throws GeneralSecurityException, IOException {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    
        // @formatter:off
        GoogleCredential credential = new GoogleCredential.Builder()
                                                .setTransport(httpTransport)
                                                .setJsonFactory(JacksonFactory.getDefaultInstance())
                                                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                                                .setServiceAccountScopes(Collections.singleton("https://www.google.com/m8/feeds"))
                                                .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
                                                .build();
        // @formatter:on
    
        if (!credential.refreshToken()) {
          throw new RuntimeException("Failed OAuth to refresh the token");
        }
    
        ContactsService myService = new ContactsService(APPLICATION_NAME);
        myService.setOAuth2Credentials(credential);
        return myService;
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题