title: 401 Unauthorized exception from Google Drive Service account authentication
body: <p>I am using google drive service account for fetching files metadata &amp; using below reference steps described in <a href="https://developers.google.com/identity/protocols/OAuth2ServiceAccount" rel="nofollow noreferrer">Using OAuth 2.0 for Server to Server Applications</a> , the code works for one gmail account only which i created 8 months before 
  but if i create new gmail account and execute below code the below Error is coming.</p>
  
  <p>We dont use any G Suite here. I am unable to identify what is the difference between OLD account and new account, because for one it works and for another it throws 401 exception as below. We have done almost same settings in developer console for both the gmail accounts.</p>
  
  <p>ERROR Trace:-</p>
  
  <pre><code>Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
  at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
  at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
  at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
  at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
  at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
  at com.demo.google.auth.GDriveServiceAcctTest.main(GDriveServiceAcctTest.java:83)
  </code></pre>
  
  <p>What i did in developer console ->
  Created new service account &amp; generate p12 file.
  Enabled Google Drive API and Admin SDK</p>
  
  <p>The inputs to the program is
  -> </p>
  
  <pre><code>private static final String SERVICE_ACCT_ID="test-XXXX@YYYY.iam.gserviceaccount.com";
  private static final String P12_FILE_PATH="D:\\XXXX-b9ryafcd4fb11c.p12";
  private static final String ACCOUNT_USER="XXXX@gmail.com";
  private static final String APPLICATION_NAME="ABCDEF";
  </code></pre>
  
  <p>Source Code:-</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(HTTP_TRANSPORT)
      .setJsonFactory(JSON_FACTORY)
      .setServiceAccountId(SERVICE_ACCT_ID)
      .setServiceAccountPrivateKeyFromP12File(new java.io.File(P12_FILE_PATH))
      .setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE))
      .setServiceAccountUser(ACCOUNT_USER)
      .build();
  
  Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
  FileList result = driveService.files().list().execute();
      List&lt;File&gt; files = result.getItems();
  
      if (files == null || files.size() == 0)
          System.out.println("No Folder found.");
      else
      {
          System.out.println("Files:" + files.size());
      folderID = files.get(0).getId();
          files = result.getItems();
          if (files == null || files.size() == 0)
                 System.out.println("No files found");
          else {
             for (File file : files)
                 {
              System.out.printf("%s (%s)\n", file.getTitle(), file.getId());
                 }
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>If anyone has idea to fix this 401 error let me know. Thanks.</p>