Package br.com.objectos.way.gdrive

Source Code of br.com.objectos.way.gdrive.AccessTokenGeneratorApplication

package br.com.objectos.way.gdrive;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.DriveScopes;
import com.google.common.io.Files;

public class AccessTokenGeneratorApplication {

  private static final String CLIENTSECRETS_LOCATION = "/client_secrets.json";

  private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

  public static void main(String[] args) throws Exception {
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(
            jsonFactory,
            new InputStreamReader(AccessTokenGeneratorApplication.class
                .getResourceAsStream(CLIENTSECRETS_LOCATION)));

    GoogleAuthorizationCodeFlow flow = new
        GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets,
            Arrays.asList(DriveScopes.DRIVE))
            .setAccessType("offline")
            .setApprovalPrompt("force").build();

    String url =
        flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
    System.out
        .println("Please open the following URL in your browser then type the authorization code:");
    System.out.println("  " + url);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String code = br.readLine();

    GoogleTokenResponse response =
        flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI)
            .execute();

    flow.createAndStoreCredential(response, null);

    java.io.File accessTokenFile = new java.io.File("src/main/resources/access_token");
    Files.write(response.getAccessToken(), accessTokenFile, Charset.forName("UTF-8"));
    System.out.println("'src/main/resources/access_token' created/updated");
    System.out.println(response.getAccessToken());
    System.out.println();

    java.io.File refreshTokenFile = new java.io.File("src/main/resources/refresh_token");
    Files.write(response.getRefreshToken(), refreshTokenFile, Charset.forName("UTF-8"));
    System.out.println("'src/main/resources/refresh_token' created/updated");
    System.out.println(response.getRefreshToken());
  }

}
TOP

Related Classes of br.com.objectos.way.gdrive.AccessTokenGeneratorApplication

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.