当前位置: 首页 > 面试题库 >

drive.changes()。watch返回GoogleJsonResponseException:401未经授权,未显示任何消息

梁昊天
2023-03-14
问题内容

我正在尝试监视Google云端硬盘中的更改,并收到401异常。在这里搜索时,我发现人们有详细的消息,尽管我一无所有,但为什么他们未被未经授权。

这是我使用的代码

public static void main(String[] args) {
    try {


        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

        // authorization
        GoogleCredential credential = GoogleCredential.getApplicationDefault()
                .createScoped(DriveScopes.all());

        boolean refreshed = credential.refreshToken();

        // set up the global Drive instance
        drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();

        Channel channel = new Channel();
        channel.setId(UUID.randomUUID().toString());
        channel.setType("web_hook");
        //my ip here
        channel.setAddress("https://com.example:");

        StartPageToken pageToken = drive.changes().getStartPageToken().execute();
        System.out.println(pageToken.getStartPageToken());
        Channel changesChannel = drive.changes().watch(pageToken.getStartPageToken(), channel).execute();
        System.out.println(changesChannel.getExpiration());

        return;
    } catch (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(1);
}

更多信息:

  1. 这是一个服务帐户。它具有所有者权限
  2. 我正在从本地计算机使用它
  3. drive.files()。list()工作正常
  4. drive.about()。get()正常工作

而且我得到的例外是:

com.google.api.client.googleapis.json.GoogleJsonResponseException:401在com.google.api.client.googleapis上的com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)未经授权。
com.google.api.client.googleapis上com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)上的services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)。
services.AbstractGoogleClientRequest $
1.interceptResponse(AbstractGoogleClientRequest.java:321)在com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed
(AbstractGoogleClientRequest.java:419),位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)的com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
.lambda.GoogleDrive2.main(MyClass.java:142)

我的pom.xml依赖项:

<dependency>
        <groupId>com.google.api-client</groupId>
        <artifactId>google-api-client</artifactId>
        <version>1.20.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.oauth-client</groupId>
        <artifactId>google-oauth-client</artifactId>
        <version>1.22.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client-extensions -->
    <dependency>
        <groupId>com.google.api-client</groupId>
        <artifactId>google-api-client-extensions</artifactId>
        <version>1.6.0-beta</version>
    </dependency>
    <dependency>
        <groupId>com.google.oauth-client</groupId>
        <artifactId>google-oauth-client-jetty</artifactId>
        <version>1.22.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-drive</artifactId>
        <version>v3-rev70-1.22.0</version>
    </dependency>

问题答案:

花了几个小时并仔细阅读了文档后,我终于找到了问题。

按照推送通知文档(https://developers.google.com/drive/v3/web/push)的规定:

要使用推送通知,您需要做三件事:

  1. 注册您的接收URL的域。…

  2. 设置您的接收URL或“ Webhook”回调接收器。

这是HTTPS服务器,用于处理资源更改时触发的API通知消息。

  1. 为您要观看的每个资源端点设置一个通知通道。

通道指定通知消息的路由信息​​。作为频道设置的一部分,您可以标识要在其中接收通知的特定URL。每当频道的资源发生变化时,Drive
API都会向该URL发送一条通知消息,作为POST请求。

这意味着您应该拥有一个域,并且应该先在Google上进行确认,然后才能使用watch API。

GoogleJsonResponseException: 401 Unauthorized言:在这种情况下,我仍然认为回应是荒谬的,但希望它能帮助其他面临相同问题的人。



 类似资料:
  • 所以,我试图用Tweepy喜欢一个推文,但是这个错误正在发生: 有什么想法吗?

  • 我正在使用postman,并尝试通过http://localhost:8180/auth/admin/realms/demo/users/{userID}更新用户的配置文件,但收到响应。 通过http://localhost:8180/auth/admin/realms/demo/users/{userID}发送带有Json内容类型和用户信息的Put请求。 不幸的是,我已经连续收到了401个回复。

  • 在这个留档Spring Security MVC Test中描述了如何使用Spring Security测试安全的ressource。我遵循了提供的所有步骤,但访问受保护的ressource仍然会返回错误代码401(未经授权)。 这是我的测试课 我的资源服务器配置: 如果你想看看整个项目,你可以在这里找到。我已经尝试了不同的测试运行程序和教程中描述的所有内容,但我找不到一个解决方案,如何在测试期间

  • 我们正在使用AWS Lambda授权器和API网关来保护我们的下游API。 下面是我们基于Java的lambda授权程序的代码片段 我们需要401(“未经授权”)作为一个回应,有人能请帮助如何做这一点吗?我们有用Java编写的lambda授权器。

  • 我的代码:GoogleCredential凭据 credential.refreshToken() 错误日志: 创建服务号的步骤: 我在凭据中的oauth 2.0中创建了一个Web应用程序 然后我用客户端ID创建了一个服务号 现在我正在使用这个服务号和从它生成的p12证书来验证和创建Google凭据的对象 一旦刷新令牌,我就给了我401例外。 在这种情况下,任何帮助都会受到感激

  • 我想使用爪哇谷歌驱动器API。我尝试了这段代码: 但是我得到了这个错误: 我使用以下配置: 你能告诉我怎么解决这个问题吗?