Guest User

Untitled

a guest
May 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. GET
  2. https://photoslibrary.googleapis.com/v1/albums
  3.  
  4. public static void main(String[] args) throws IOException, GeneralSecurityException, ServiceException, ParseException {
  5. GoogleCredential credential = createCredential();
  6.  
  7. if (!credential.refreshToken()) {
  8. throw new RuntimeException("Failed OAuth to refresh the token");
  9. }
  10.  
  11. System.out.println(credential.getAccessToken());
  12. doGetRequest(credential.getAccessToken(), "https://photoslibrary.googleapis.com/v1/albums");
  13. }
  14.  
  15.  
  16. private static GoogleCredential createCredential() {
  17. try {
  18. return new GoogleCredential.Builder()
  19. .setTransport(HTTP_TRANSPORT)
  20. .setJsonFactory(JSON_FACTORY)
  21. .setServiceAccountId(emailAccount)
  22. .setServiceAccountPrivateKeyFromP12File(ENCRYPTED_FILE)
  23. .setServiceAccountScopes(SCOPE)
  24. .setServiceAccountUser(emailAccount)
  25. .build();
  26. } catch (Exception e) {
  27. throw new RuntimeException("Error while creating Google credential");
  28. }
  29. }
  30.  
  31. private static void doGetRequest(String accessToken, String url) throws IOException, ParseException {
  32. logger.debug("doGetRequest, with params: url: {}, access token: {}", accessToken, url);
  33. HttpGet get = new HttpGet(url);
  34. get.addHeader("Authorization",
  35. "Bearer" + " " + accessToken);
  36. HttpClient client = HttpClientBuilder.create().build();
  37. HttpResponse response = client.execute(get);
  38. String json = EntityUtils.toString(response.getEntity());
  39. System.out.println(json);
  40. }
Add Comment
Please, Sign In to add comment