GoogleJsonResponseException:在获取企业用户列表期间找不到404

时间:2015-09-03 18:53:05

标签: java google-api google-api-java-client

我正在使用Google Play EMM API(lib google-api-services-androidenterprise-v1-rev8-1.20.0.jar)编写简单应用程序,该应用程序应生成用于自动安装设备策略控制器的激活码(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$1.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()返回null。

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

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

进行准备

0 个答案:

没有答案