Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GET
- https://photoslibrary.googleapis.com/v1/albums
- public static void main(String[] args) throws IOException, GeneralSecurityException, ServiceException, ParseException {
- GoogleCredential credential = createCredential();
- if (!credential.refreshToken()) {
- throw new RuntimeException("Failed OAuth to refresh the token");
- }
- System.out.println(credential.getAccessToken());
- doGetRequest(credential.getAccessToken(), "https://photoslibrary.googleapis.com/v1/albums");
- }
- private static GoogleCredential createCredential() {
- try {
- return new GoogleCredential.Builder()
- .setTransport(HTTP_TRANSPORT)
- .setJsonFactory(JSON_FACTORY)
- .setServiceAccountId(emailAccount)
- .setServiceAccountPrivateKeyFromP12File(ENCRYPTED_FILE)
- .setServiceAccountScopes(SCOPE)
- .setServiceAccountUser(emailAccount)
- .build();
- } catch (Exception e) {
- throw new RuntimeException("Error while creating Google credential");
- }
- }
- private static void doGetRequest(String accessToken, String url) throws IOException, ParseException {
- logger.debug("doGetRequest, with params: url: {}, access token: {}", accessToken, url);
- HttpGet get = new HttpGet(url);
- get.addHeader("Authorization",
- "Bearer" + " " + accessToken);
- HttpClient client = HttpClientBuilder.create().build();
- HttpResponse response = client.execute(get);
- String json = EntityUtils.toString(response.getEntity());
- System.out.println(json);
- }
Add Comment
Please, Sign In to add comment