drive.changes()。watch返回GoogleJsonResponseException:401 Unauthorized,没有任何消息

时间:2017-05-02 05:43:53

标签: java google-drive-api google-api-java-client

我正在尝试关注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()工作正常
  5. 我得到的例外:

      

    com.google.api.client.googleapis.json.GoogleJsonResponseException:401 Unauthorized       在com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)       在com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)       在com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)       在com.google.api.client.googleapis.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.executeUnparsed(AbstractGoogleClientRequest.java:352)       在com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)       在main.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>
    

1 个答案:

答案 0 :(得分:0)

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

正如推送通知文档(https://developers.google.com/drive/v3/web/push)中所述:

  

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

     
      
  1. 注册接收网址的域名。 ...

  2.   
  3. 设置接收网址,或者#34; Webhook&#34;回叫接收器。

         

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

  4.   
  5. 为要监视的每个资源端点设置通知通道。

         

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

  6.   

这意味着你应该有一个域名,你应该在google上确认它,然后才能使用watch API。

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