title: Using Java and OAuth 2.0 to Connect to Google Contacts
body: <p>I am having issues connecting to the Google Contacts API with Java using OAuth 2.0. After extensive searching, I have reached the point where it appears I can connect, but receive error messages in the final process.</p>
  
  <p>I have done the following:</p>
  
  <p>1) Created an OAuth 2.0 Service account via Google's Developers Console for the project.</p>
  
  <p>2) Following the steps to 'Delegate domain-wide authority to the service account' I found at <a href="https://developers.google.com/identity/protocols/OAuth2ServiceAccount" rel="nofollow">https://developers.google.com/identity/protocols/OAuth2ServiceAccount</a>, I have authorized the Client ID of the service account created in step #1 above to the scope <a href="https://www.google.com/m8/feeds/" rel="nofollow">https://www.google.com/m8/feeds/</a> for the Contacts (Read/Write).</p>
  
  <p>3) Here is the code I have based on other members posts:</p>
  
  <pre><code>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.IOException;
  import java.security.GeneralSecurityException;
  import java.util.Arrays;
  
  public class Connector {
  private static ContactsService contactService = null;
  private static HttpTransport httpTransport;
  
  private static final String APPLICATION_NAME = "MyProject";
  private static final String SERVICE_ACCOUNT_EMAIL = "service-account-email-address@developer.gserviceaccount.com";
  private static final java.util.List&lt;String&gt; SCOPE = Arrays.asList("https://www.google.com/m8/feeds");
  
  private Connector() {
      // explicit private no-args constructor
  }
  
  public static ContactsService getInstance() {
      if (contactService == null) {
          try {
              contactService = connect();
          } catch (GeneralSecurityException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      return contactService;
  }
  
  private static ContactsService connect() throws GeneralSecurityException, IOException {
      httpTransport = GoogleNetHttpTransport.newTrustedTransport();
  
      java.io.File p12File = new java.io.File("MyProject.p12");
  
      // @formatter:off
      GoogleCredential credential = new GoogleCredential.Builder()
                                                      .setTransport(httpTransport)
                                                      .setJsonFactory(JacksonFactory.getDefaultInstance())
                                                      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                                                      .setServiceAccountScopes(SCOPE)
                                                      .setServiceAccountPrivateKeyFromP12File(p12File)
                                                      .setServiceAccountUser("user@example.com")
                                                      .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;
  }
  }
  </code></pre>
  
  <p>In theory, I believe this is supposed to all work. However, when <strong><em>credential.refreshToken()</em></strong> is executed I receive the error:</p>
  
  <pre><code>com.google.api.client.auth.oauth2.TokenResponseException: 403 Forbidden
  {
    "error" : "access_denied",
    "error_description" : "Requested client not authorized."
  }
  </code></pre>
  
  <p>If I remove <strong><em>setServiceAccountUser("user@example.com")</em></strong>, I receive the error message <strong><em>com.google.gdata.util.ServiceForbiddenException: Cannot request contacts belonging to another user</em></strong>. I believe I need to keep setServiceAccountUser(...) but am unable to get past the token response exception.</p>
  
  <p>I am unsure where to go from here.</p>
  
  <p>Thank you for any advice or assistance you can provide.</p>