Lechucico Lechucico - 1 year ago 120
Java Question

Google Drive web-hook configuration

I'm reading the google drive documentation but It's a bit unclear:


  • I have to set the web-hook for each user on my application, or only once?

  • There is any example of this configuration in java?

  • How I can retrieve th changes for my users(maybe a cursor)?



Here is how I let users authenticate to my application:

@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();
}


How I can set webhooks?

Answer Source

You may check this documentation on how to create webhooks. Webhooks can be delivered using different content types:

  • The application/json content type will deliver the JSON payload directly as the body of the POST.
  • The application/x-www-form-urlencoded content type will send the JSON payload as a form parameter called "payload".

Regarding how you can retrieve the changes for your users, you may use the push notifications that inform your application when a resource changes. To request push notifications, you need to set up a notification channel for each resource you want to watch. After your notification channels are set up, the Drive API will inform your application when any watched resource changes.

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.

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download