Google 驱动 web-hook 配置
Google Drive web-hook configuration
我正在阅读 google 驱动器文档,但有点不清楚:
- 我必须为我的应用程序中的每个用户设置网络挂钩,还是只设置一次?
- 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();
}
如何设置 webhooks?
您可以查看此 documentation 以了解如何创建 webhook。可以使用不同的内容类型传送 Webhook:
application/json
内容类型将 JSON 负载直接作为 POST 的正文传送。
application/x-www-form-urlencoded
内容类型将 JSON 负载作为名为 "payload" 的表单参数发送。
关于如何为您的用户检索更改,您可以使用 push notifications 在资源更改时通知您的应用程序。要请求推送通知,您需要为每个要观看的资源设置一个通知通道。设置通知渠道后,Drive API 将在任何监视的资源发生变化时通知您的应用程序。
Use the changes.watch
method to subscribe for updates to the change log. Notifications do not contain details about the changes. Instead, they indicate that new changes are available. To retrieve the actual changes, poll the change feed as described in Retrieving changes.
希望对您有所帮助!
我正在阅读 google 驱动器文档,但有点不清楚:
- 我必须为我的应用程序中的每个用户设置网络挂钩,还是只设置一次?
- 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();
}
如何设置 webhooks?
您可以查看此 documentation 以了解如何创建 webhook。可以使用不同的内容类型传送 Webhook:
application/json
内容类型将 JSON 负载直接作为 POST 的正文传送。application/x-www-form-urlencoded
内容类型将 JSON 负载作为名为 "payload" 的表单参数发送。
关于如何为您的用户检索更改,您可以使用 push notifications 在资源更改时通知您的应用程序。要请求推送通知,您需要为每个要观看的资源设置一个通知通道。设置通知渠道后,Drive API 将在任何监视的资源发生变化时通知您的应用程序。
Use the
changes.watch
method to subscribe for updates to the change log. Notifications do not contain details about the changes. Instead, they indicate that new changes are available. To retrieve the actual changes, poll the change feed as described in Retrieving changes.
希望对您有所帮助!