ammu
ammu

Reputation: 864

401 Unauthorized exception from Google Drive Service account authentication

I am using google drive service account for fetching files metadata & using below reference steps described in Using OAuth 2.0 for Server to Server Applications , 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.

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.

ERROR Trace:-

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)

What i did in developer console -> Created new service account & generate p12 file. Enabled Google Drive API and Admin SDK

The inputs to the program is ->

private static final String SERVICE_ACCT_ID="[email protected]";
private static final String P12_FILE_PATH="D:\\XXXX-b9ryafcd4fb11c.p12";
private static final String ACCOUNT_USER="[email protected]";
private static final String APPLICATION_NAME="ABCDEF";

Source 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<File> 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());
               }
        }
    }

}

If anyone has idea to fix this 401 error let me know. Thanks.

Upvotes: 3

Views: 1439

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

Remember service accounts are not you. A service account must be preauthorized to access the drive account. You need to share the directories or files with the service account you want it to be able to access like you would any user.

Upvotes: 1

Related Questions