title: Obtaining OAuth credentials for Google Drive API
body: <p>I am trying to get access to some files on google drive via an application I am developing. Unfortunately I am having difficulties in obtaining the credentials. I have created an Google Service Account and the provided email-Address and private key should be ok.
  However when calling the method <code>getDriveService()</code> from below, I am receiving the error</p>
  
  <pre><code>java.lang.NullPointerException
      at java.util.Collections$UnmodifiableCollection.&lt;init&gt;(Unknown Source)
      at java.util.Collections.unmodifiableCollection(Unknown Source)
      at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.&lt;init&gt;(GoogleCredential.java:321)
      at com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder.build(GoogleCredential.java:515)
  </code></pre>
  
  <p>Am I doing something obvious wrong here?</p>
  
  <p>My code so far:</p>
  
  <pre><code>import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
  import com.google.api.client.http.HttpTransport;
  import com.google.api.client.http.javanet.NetHttpTransport;
  import com.google.api.client.json.JsonFactory;
  import com.google.api.client.json.jackson2.JacksonFactory;
  import com.google.api.services.drive.Drive;
  import com.google.api.services.drive.DriveScopes;
  
  import java.security.GeneralSecurityException;
  import java.io.IOException;
  import java.net.URISyntaxException;
  
  private static final String SERVICE_ACCOUNT_EMAIL = "VERY_LONG_ID@developer.gserviceaccount.com";
  private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/home/user/googledriveKey.p12";
  
  public static Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException {
      HttpTransport httpTransport = new NetHttpTransport();
      JacksonFactory jsonFactory = new JacksonFactory();
      GoogleCredential credential = new GoogleCredential.Builder()
          .setTransport(httpTransport)
          .setJsonFactory(jsonFactory)
          .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
          .setServiceAccountPrivateKeyFromP12File(
              new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
          .build();
      Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
          .setHttpRequestInitializer(credential).build();
      return service;
  }
  </code></pre>