public static GoogleCredential getGoogleCredential() throws Exception { if (_googleCredential != null) { return _googleCredential; } GoogleCredential.Builder builder = new GoogleCredential.Builder(); builder.setJsonFactory(new JacksonFactory()); builder.setServiceAccountId(PortletPropsValues.GOOGLE_API_SERVICE_ACCOUNT_ID); File file = new File( PropsUtil.get(PropsKeys.LIFERAY_HOME) + PortletPropsValues.GOOGLE_API_SERVICE_ACCOUNT_PRIVATE_KEY_P12_FILE); builder.setServiceAccountPrivateKeyFromP12File(file); builder.setServiceAccountScopes( Arrays.asList(PortletPropsValues.GOOGLE_API_SERVICE_ACCOUNT_SCOPES)); builder.setServiceAccountUser(PortletPropsValues.GOOGLE_API_SERVICE_ACCOUNT_USER); builder.setTransport(new NetHttpTransport()); _googleCredential = builder.build(); return _googleCredential; }
@SuppressWarnings({"oracle.jdeveloper.java.semantic-warning", "deprecation"}) private Credential authorize() throws Exception { HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder builder = new com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder(); builder.setTransport(HTTP_TRANSPORT); builder.setJsonFactory(JSON_FACTORY); builder.setServiceAccountId(getServiceAccountEmail()); builder.setServiceAccountScopes(new String[] {"https://www.googleapis.com/auth/analytics"}); builder.setServiceAccountPrivateKeyFromP12File(getServiceAccountPrivateKey().getFile()); GoogleCredential credential = builder.build(); return credential; }