Авторизовать Google My Business API в Java

Я настроил Google My Businees API и могу получать отзывы с помощью Google Oauthplayground. Теперь я хотел запустить это на моей локальной машине. Я создал образец приложения на своем локальном компьютере, как описано в следующем URL-адресе https://developers.google.com/my-business/content/set-up-java-client.

private static final java.io.File DATA_STORE_DIR =
    new java.io.File(System.getProperty("user.home"),
    ".store/mybusiness_sample");
private static FileDataStoreFactory dataStoreFactory;
private static HttpTransport httpTransport;
private static final JsonFactory JSON_FACTORY =
    JacksonFactory.getDefaultInstance();
private static Mybusiness mybusiness;

/**
 * Demonstrates the authentication flow to use
 * with the Google My Business API Java client library.
 * @return AuthorizationCodeInstalledApp
 */
private static Credential authorize() throws Exception {
  // Creates an InputStream to hold the client ID and secret.
  InputStream secrets = Main.class.getResourceAsStream("/client_secrets.json");

  // Prompts the user if no credential is found.
  if (secrets == null) {
    System.out.println(∏
      "Enter Client ID and Secret from Google API Console "
        + "into google-my-business-api-sample/src/main/resources/client_secrets.json");
    System.exit(1);
  }

  // Uses the InputStream to create an instance of GoogleClientSecrets.
  GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
      new InputStreamReader(secrets));
  if (clientSecrets.getDetails().getClientId().startsWith("Enter")
      || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
    System.out.println(
        "Enter Client ID and Secret from Google API Console "
            + "into google-my-business-api-sample/src/main/resources/client_secrets.json");
    System.exit(1);
  }

  // Sets up the authorization code flow.
  GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
      httpTransport, JSON_FACTORY, clientSecrets,
      Collections.singleton("https://www.googleapis.com/auth/plus.business.manage"))
      .setDataStoreFactory(dataStoreFactory).build();
  // Returns the credential.
  LocalServerReceiver localReceiver = new LocalServerReceiver.
              Builder().setPort(51551).build();
  return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}

public static void main(String[] args) throws Exception {
  httpTransport = GoogleNetHttpTransport.newTrustedTransport();
  dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

  // Calls the authorize() function to get a credential.
  Credential credential = authorize();

  // Calls Mybusiness.Builder to create a new instance named 'mybusiness'.
  mybusiness = new Mybusiness.Builder(httpTransport, JSON_FACTORY, credential)
      .setApplicationName(APPLICATION_NAME).build();

  // Uses the 'mybusiness' instance to send an API call.
  Mybusiness.Accounts.List accountsList = mybusiness.accounts().list();
  ListAccountsResponse response = accountsList.execute();
  List accounts = response.getAccounts();
}

Но когда я запустил приложение, при возврате учетных данных в методе authorize() открывается окно браузера, и я должен войти в систему и авторизовать приложение через браузер. После этого приложение останавливается. Возможно ли запустить приложение без вмешательства браузера, так как я уже указал client_id и секрет в самой программе?

Пожалуйста помоги мне с этим.

0 ответов

Другие вопросы по тегам