com.google.api.client.auth.oauth2.TokenResponseException:401未经授权 [英] com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized

查看:944
本文介绍了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.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(凭证).build();

退货服务;
}

但是我得到这个错误:

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

我使用此配置:





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

解决方案

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



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


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.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 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

这篇关于com.google.api.client.auth.oauth2.TokenResponseException:401未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆