我想使用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