public class SampleServlet extends AbstractAppEngineAuthorizationCodeServlet {
private final static Logger logger = Logger.getLogger(SampleServlet.class.getName());
private static final long serialVersionUID = 1L;
// Constants omitted
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
AuthorizationCodeFlow authFlow = initializeFlow();
Credential credential = authFlow.loadCredential(getUserId(req));
StringBuilder resultFromWatch = new StringBuilder();
Drive drive = new Drive.Builder(Utils.HTTP_TRANSPORT, Utils.JSON_FACTORY, credential).setApplicationName("t").build();
try {
Optional<Channel> channel = watchFile(drive, FILE_ID, CHANNEL_ID, "web_hook", PUSH_URL);
String channelStringTmp;
if (channel.isPresent()) {
channelStringTmp = channel.get().toString();
} else {
channelStringTmp = "null...";
}
resultFromWatch.append(channelStringTmp);
} catch (Exception e) {
resultFromWatch.append(e.getMessage());
}
final UserService userService = UserServiceFactory.getUserService();
final String thisUrl = req.getRequestURI();
// Send the results as the response
PrintWriter respWriter = resp.getWriter();
resp.setStatus(200);
resp.setContentType("text/html");
addLoginLogoutButtons(req, resp, resultFromWatch, userService, thisUrl, respWriter);
}
private static Optional<Channel> watchFile(Drive service, String fileId,
String channelId, String channelType, String channelAddress) throws IOException {
final Channel returnValue;
Channel channel = new Channel();
channel.setId(channelId);
channel.setType(channelType);
channel.setAddress(channelAddress);
Drive.Files tmp = service.files();
returnValue = tmp.watch(fileId, channel).execute();
return Optional.fromNullable(returnValue);
}
@Override
protected AuthorizationCodeFlow initializeFlow() throws ServletException, IOException {
return Utils.initializeFlow();
}
@Override
protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
return Utils.getRedirectUri(req);
}
}
{
"expiration": "1484565747000",
"id": SAME_ID_AS_DEFINED_IN_SERVLET,
"kind": "api#channel",
"resourceId": A_NEW_ID,
"resourceUri": "https:\/\/www.googleapis.com\/drive\/v3\/files\/FILE_ID?acknowledgeAbuse=false&alt=json"
}
@RestController
@RequestMapping(/*PUSH_URL*/)
public class ConcreteFileWatchController implements FileWatchController {
private final static Logger logger = Logger.getLogger(ConcreteFileWatchController.class.getName());
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
logger.info("Received watch call");
}
}
After having set-up everything I'm getting error:
...
errors": [
{
"domain": "global",
"reason": "push.webhookUrlUnauthorized",
"message": "Unauthorized WebHook callback channel: https://app.example.com"
}
],
...