com.google.api.client.auth.oauth2.TokenResponseException:401未经授权

编程入门 行业动态 更新时间:2024-10-26 09:27:00
本文介绍了com.google.api.client.auth.oauth2.TokenResponseException:401未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用Java Google Drive API。 我试过这段代码: pre $ public $ getDriveService()抛出GeneralSecurityException,IOException,URISyntaxException { HttpTransport httpTransport = new NetHttpTransport(); JacksonFactory JSON_FACTORY = new JacksonFactory(); GoogleCredential凭证=新的GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(JSON_FACTORY) .setServiceAccountId(sonoratestw-226 @ sonora-project .iam.gserviceaccount) .setServiceAccountPrivateKeyFromP12File(new java.io.File(C:\\buffer\\sonora project-3256770463ed.p12)) .setServiceAccountScopes( Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN)) .setServiceAccountUser(sonoratestw@gmail) .build(); Drive service = new Drive.Builder(httpTransport,JSON_FACTORY,null) .setApplicationName(FileListAccessProject) .setHttpRequestInitializer(凭证).build(); 退货服务; }

但是我得到这个错误:

发生错误:com.google.api.client.auth.oauth2.TokenResponseException:401未授权

我使用此配置:

你可以给我一些想法,我可以解决这个问题吗? 解决方案

一个href =stackoverflow/questions/29277025> SO问题,您得到错误401的原因之一是您正在使用的服务帐户无法访问关联的驱动器帐户与您的Gmail地址。所以,如果你想能够访问它上传的文件,你必须授予自己通过服务帐户访问他们的权限。

以下是我认为可以解决的其他SO问题帮助您更好地了解服务帐户。

  • google oauth2用user@gmail冒充服务帐户

  • Google驱动api服务帐户视图使用java将上传的文件上传到Google Drive

I want to use Java Google Drive API. 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") .setServiceAccountPrivateKeyFromP12File(new java.io.File("C:\\buffer\\sonora project-3256770463ed.p12")) .setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN)) .setServiceAccountUser("sonoratestw@gmail") .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 use this configuration:

Can you give some idea how I can fix this?

解决方案

Based on this SO question, one reason that you get the error 401 is that the service account you are using doesn't have access to the drive account associated with your Gmail address. So if you want to be able to access the files it uploads you must grant yourself access to them though the service account.

Here are the other SO questions that I think can help you to understand more the service account

  • google oauth2 impersonate service account with user@gmail

  • Google drive api services account view uploaded files to google drive using java

  • Google Drive changing permissions by service account

更多推荐

com.google.api.client.auth.oauth2.TokenResponseException:401未经授权

本文发布于:2023-11-09 22:44:25,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未经授权   client   api   google   auth

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!