public static Credential getComputeEngineCredential()
throws GeneralSecurityException, IOException {
NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
try {
// Try to connect using Google Compute Engine service account credentials.
ComputeCredential credential = new ComputeCredential(transport, new JacksonFactory());
// Force token refresh to detect if we are running on Google Compute Engine.
credential.refreshToken();
return credential;
} catch (IOException e) {
return null;
}
}