我想使用Java谷歌雲端硬盤API。 我試過這段代碼:com.google.api.client.auth.oauth2.TokenResponseException:401未經授權
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("[email protected]")
.setServiceAccountPrivateKeyFromP12File(new java.io.File("C:\\buffer\\sonora project-3256770463ed.p12"))
.setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
.setServiceAccountUser("[email protected]")
.build();
Drive service = new Drive.Builder(httpTransport, JSON_FACTORY, null)
.setApplicationName("FileListAccessProject")
.setHttpRequestInitializer(credential).build();
return service;
}
但我得到這個錯誤:
An error occurred: com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
我使用這種配置:
您能否給一些想法,我怎麼能解決這個問題?
我不另做題沒想到你能請使用setServiceAccountUser,除非它的域管理員帳戶已將服務帳戶添加到管理員帳戶,並且相關用戶也是管理員帳戶上的用戶。 – DaImTo
你可以給它總拋出的錯誤和異常嗎? – Coder