GoogleJsonResponseException:获取企业用户列表时404 Not Found

GoogleJsonResponseException: 404 Not Found during getting user list of enterprise

我正在使用 Google Play EMM API (lib google-api-services-androidenterprise-v1-rev8-1.20.0.jar) 应该生成激活码的简单应用程序,用于在设备上自动安装 Device Policy Controller(Android for Work)。

HttpTransport sHttpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory sJsonFactory = JacksonFactory.getDefaultInstance();

GoogleCredential credentials = new GoogleCredential.Builder().setTransport(sHttpTransport).setJsonFactory(sJsonFactory)
        .setServiceAccountId(MASTER_SERVICE_ACCOUNT_EMAIL)
        .setServiceAccountScopes(AndroidEnterpriseScopes.all())
        .setServiceAccountPrivateKeyFromP12File(new File(PRIVATE_KEY_P12_FILE_NAME))
        .build();

AndroidEnterprise sAndroidEnterprise = new AndroidEnterprise(sHttpTransport, sJsonFactory, credentials);

String userId = sAndroidEnterprise.users().list(enterpriseId, userEmail).execute()
            .getUser()
            .get(0)
            .getId();

String activationCode = sAndroidEnterprise.users().generateToken(enterpriseId, userId).execute().getToken();

问题是我在

中的方法 execute() 中得到异常结果
String userId = sAndroidEnterprise.users().list(enterpriseId, userEmail).execute()

com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "location" : "enterpriseId",
    "locationType" : "parameter",
    "message" : "No enterprise was found for the given id.",
    "reason" : "notFound"
  } ],
  "message" : "No enterprise was found for the given id."
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

最有趣的是,如果我将不存在的电子邮件作为参数 userEmail,则在方法 execute() 中没有异常,当然方法 getUser() returns null。

如何修复此代码以成功获取用户 ID? 对我来说,它看起来像是图书馆或服务器的问题,但我希望我只是做错了什么。

请注意:我已经成功注册了一个客户,并根据https://developers.google.com/play/enterprise/samples

做了准备

您似乎是在使用 MSA 帐户通过身份验证调用 users() 方法。尝试使用 ESA 帐户进行身份验证,即您在请求中提到的企业 ID。 (您需要为客户创建服务帐户才能进行身份验证)。问题将得到解决。