我已经配置了 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 和 secret?
请帮助我。