Google Drive网络挂钩配置

时间:2017-07-24 08:51:44

标签: java google-drive-api webhooks

我正在阅读谷歌驱动器文档,但有点不清楚:

  • 我必须在我的应用程序上为每个用户设置web-hook,或者只设置一次?
  • java中有这个配置的任何例子吗?
  • 如何检索用户的更改(可能是光标)?

以下是我让用户对我的应用程序进行身份验证的方法:

@GET    
@Path("/start")
public void start(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException {

    String url = initFlow().newAuthorizationUrl().setRedirectUri("http://localhost:8080/GDriveRest/app/gdrive/finish").build();
    response.sendRedirect(url);
}

@GET
@Path("/finish")
public void finish(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException {

    AuthorizationCodeFlow flow = initFlow();
    flow.newTokenRequest(request.getParameter("code"));

    response.sendRedirect("http://m.memegen.com/1yx6o5.jpg?"+request.getParameter("code")+"&id="+flow.getClientId());
}

private AuthorizationCodeFlow initFlow() throws IOException {

    InputStream in = GDrive.class.getResourceAsStream("/client_secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    return new GoogleAuthorizationCodeFlow.Builder(new NetHttpTransport(), 
                                                   JacksonFactory.getDefaultInstance(), 
                                                   clientSecrets, SCOPES).setAccessType("offline").build();
}

如何设置webhook?

1 个答案:

答案 0 :(得分:1)

您可以查看documentation有关如何创建webhook的信息。 Webhooks可以使用不同的内容类型提供:

  • application/json内容类型将直接作为POST主体提供JSON有效内容。
  • application/x-www-form-urlencoded内容类型会将JSON有效内容作为名为“payload”的表单参数发送。

关于如何为用户检索更改,您可以使用push notifications在资源发生变化时通知您的应用程序。要请求推送通知,您需要为要监视的每个资源设置通知通道。设置通知渠道后,当任何观看的资源发生变化时,Drive API会通知您的应用程序。

  

使用changes.watch方法订阅更改日志的更新。通知不包含有关更改的详细信息。相反,它们表明可以进行新的更改。要检索实际更改,请按Retrieving changes

中所述轮询更改Feed

希望这有帮助!