我正在尝试使用Java中的服务帐户从我的Google商家帐户中获取所有帐户列表。
private static HttpTransport httpTransport;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static MyBusiness mybusiness;
public static GoogleCredential getCredential() {
GoogleCredential credential = null;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// check for valid setup
if (UrlConstant.SERVICE_ACCOUNT_EMAIL.startsWith("Enter ")) {
System.err.println(UrlConstant.SERVICE_ACCOUNT_EMAIL);
System.exit(1);
}
String p12Content = Files.readFirstLine(new java.io.File(UrlConstant.AUTH_FILE_PATH),
Charset.defaultCharset());
if (p12Content.startsWith("Please")) {
System.err.println(p12Content);
System.exit(1);
}
credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
.setServiceAccountId(UrlConstant.SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/plus.business.manage"))
.setServiceAccountPrivateKeyFromP12File(new java.io.File(UrlConstant.AUTH_FILE_PATH))
.build();
} catch (Exception e) {
}
return credential;
}
public static void main(String[] args) throws IOException {
GoogleCredential credential = ServerAunthication.getCredential();
System.out.println(credential);
// Calls Mybusiness.Builder to create a new instance named 'mybusiness'.
mybusiness = new MyBusiness.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(
"test").build();
com.google.api.services.mybusiness.v3.MyBusiness.Accounts.List accountsList = mybusiness.accounts().list();
ListAccountsResponse response1 = accountsList.execute();
List accounts = response1.getAccounts();
但是低于错误:
找不到服务mybusiness.googleapis.com
的方法列表帐户