Используйте API YouTube с несколькими аккаунтами - PullRequest
0 голосов
/ 04 февраля 2020

Я успешно использую приведенный ниже код для подключения к API YouTube:

public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {

    InputStream in = GoogleSheetImpl.class.getResourceAsStream("/client_secret_json");

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
    DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore)
            .build();
    LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
    return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}

private static YouTube youtube;

@Test
public void test() {

    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");

    try {
        Credential credential = authorize(scopes, "commentthreads");

        youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName("youtube-cmdline-playlistupdates-sample")
                .build();

        process();

    } catch (GoogleJsonResponseException e) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}

Но, как вы можете видеть, он использует OAuth для аутентификации, которая открывает веб-страницу и просит меня войти в мою учетную запись Google.

Я попытался сделать комментарий к какому-то видео, а после этого со вторым аккаунтом сделать дополнительный комментарий к какому-либо видео. Но почему-то я вижу комментарии одного пользователя. Я заметил, что учетные данные кэшируются в файл. Есть ли способ использовать больше, чем один пользователь, чтобы комментировать видео?

Есть ли способ отключить кэширование учетных данных?

...