Учетные данные хранилища API Google Диска для Java

avatar
Wani
8 августа 2021 в 16:29
475
1
1

Я разрабатываю приложение с весенней загрузкой, которому необходимо загружать файлы в учетную запись Google Диска. Я нашел ветку для хранения access_token и токена обновления, но она устарела, вот ссылка: Как локально хранить учетные данные для SDK Google Диска. Есть ли способ использовать v3 для однократной реализации аутентификации Google Диска и обновления токена доступа при необходимости?

Ниже показан сервис, загружающий файлы на диск

@Service
public class GoogleDriveService {
    private static final String baseConfigPath = "/config.json";

    private static final String APPLICATION_NAME = "application name";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);
    private static final String CREDENTIALS_FILE_PATH = "/client_secret.json";

    /////////////////////////////////////////////////////////////////////////////////////////

    public static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = GoogleDriveClient.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();


        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost("127.0.0.1").setPort(8089).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }


    public boolean uploadGoogleDriveFile(java.io.File originalFile){

        final NetHttpTransport HTTP_TRANSPORT;
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                    .setApplicationName(APPLICATION_NAME)
                    .build();

            File file = new File();
            file.setName(originalFile.getName());

            FileContent content = new FileContent("text/plain", originalFile);

            File uploadedFile  = service.files().create(file, content).setFields("id").execute();
            System.out.println("File ID: " + uploadedFile.getId());

            return true;
        } catch (GeneralSecurityException | IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
Источник

Ответы (1)

avatar
DaImTo
8 августа 2021 в 16:40
2

Что-то вроде этого должно быть тем, что вы ищете, оно должно хранить кредиты для вас для пользователя.

/**
 * A simple example of how to access the Google Drive API.
 */
public class HelloDrive {
  // Path to client_secrets.json file downloaded from the Developer's Console.
  // The path is relative to HelloDrive.java.
  private static final String CLIENT_SECRET_JSON_RESOURCE = "client_secrets.json";

  // Replace with your view ID.
  private static final String VIEW_ID = "<REPLACE_WITH_VIEW_ID>";

      // The directory where the user's credentials will be stored.
      private static final File DATA_STORE_DIR = new File(
          System.getProperty("user.home"), ".store/hello_drive");
    
      private static final String APPLICATION_NAME = "Hello Google drive";
      private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
      private static NetHttpTransport httpTransport;
      private static FileDataStoreFactory dataStoreFactory;
    
      public static void main(String[] args) {
        try {
          Drive service = initializeDrive();
    
          
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
   /**
   * Initializes an authorized Drive service object.
   *
   * @return The Drive service object.
   * @throws IOException
   * @throws GeneralSecurityException
   */
  private static Drive initializeDrive() throws GeneralSecurityException, IOException {

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

    // Load client secrets.
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(HelloDrive.class
            .getResourceAsStream(CLIENT_SECRET_JSON_RESOURCE)));

    // Set up authorization code flow for all authorization scopes.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow
        .Builder(httpTransport, JSON_FACTORY, clientSecrets,
            DriveScopes.all()).setDataStoreFactory(dataStoreFactory)
        .build();

    // Authorize.
    Credential credential = new AuthorizationCodeInstalledApp(flow,
        new LocalServerReceiver()).authorize("user");
    // Construct the Drive service object.
    return new Drive.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APPLICATION_NAME).build();
  }

На основе Google Analytics образца