Com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized

I want to use the Java API of Google Drive. I tried this code:

public Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException { HttpTransport httpTransport = new NetHttpTransport(); JacksonFactory JSON_FACTORY = new JacksonFactory(); GoogleCredential credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(JSON_FACTORY) .setServiceAccountId(" sonoratestw-226@sonora-project.iam.gserviceaccount.com ") .setServiceAccountPrivateKeyFromP12File(new java.io.File("C:\\buffer\\sonora project-3256770463ed.p12")) .setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN)) .setServiceAccountUser(" sonoratestw@gmail.com ") .build(); Drive service = new Drive.Builder(httpTransport, JSON_FACTORY, null) .setApplicationName("FileListAccessProject") .setHttpRequestInitializer(credential).build(); return service; } 

But I get this error:

 An error occurred: com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized 

I am using this configuration:

enter image description here

Can you give an idea of ​​how I can fix this?

+5
source share
1 answer

Based on this question from fooobar.com/questions/1260559 / ... , one of the reasons you get error 401 is because the service account you are using does not have access to the disk account associated with your gmail address. Therefore, if you want to have access to the files that you upload, you must give them access to them, although the service account.

Here are other SO questions that I think will help you better understand the service account

0
source

Source: https://habr.com/ru/post/1260551/


All Articles