400状态"无效授权"在Java

时间:2015-08-03 20:48:03

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

我有一个后端流程,尝试自动创建工作的Google Apps用户(据我所知,没有任何更改)直到上周。与其他问题类似的其他问题的代码似乎与我的不相关或太不同,因为我们认为代码是有用的。

以下代码是用于生成帐户的假设

            private static String GMAIL_DOMAIN = "obfuscated.com";
            private static String GAPPS_EMAIL = "1234567890-a978g6e789gage96gaf6ag997gawg@developer.gserviceaccount.com";
            private static JsonFactory JSON_FACTORY = JacksonFactory
                .getDefaultInstance();

            HttpTransport httpTransport = GoogleNetHttpTransport
                    .newTrustedTransport();
            List<String> scopes = new ArrayList<String>();
            scopes.add(DirectoryScopes.ADMIN_DIRECTORY_USER);
            scopes.add(DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER);
            File p12 = new File("/opt/gapps.p12");
            GoogleCredential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport).setJsonFactory(JSON_FACTORY)
                    .setServiceAccountId(GAPPS_EMAIL)
                    .setServiceAccountPrivateKeyFromP12File(p12)
                    .setServiceAccountScopes(scopes)
                    .setServiceAccountUser("obfus@cated.com")
                    .build();
            Directory service = new Directory.Builder(httpTransport,
                    JSON_FACTORY, credential).setApplicationName("Obfuscated")
                    .build();

            // All of these local variables are set further up in the code.
            com.google.api.services.admin.directory.model.User gappsUser = new com.google.api.services.admin.directory.model.User();
            UserName gappsUsername = new UserName();
            gappsUsername.setGivenName(fname);
            gappsUsername.setFamilyName(lname);
            gappsUsername.setFullName(fname + " " + lname);
            gappsUser.setName(gappsUsername);
            gappsUser.setPrimaryEmail(username);
            gappsUser.setPassword(password);

            Directory.Users.Insert gappsRequest = service.users().insert(
                    gappsUser);
            gappsRequest.execute();

以下是返回的截断堆栈跟踪(我的类所示的行是最后一行):

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{ "error" : "invalid_grant" } at
com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at
com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at
com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at
com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
at
com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at
com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859) 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)

我已经确认p12文件仍然存在,并且就服务帐户凭据而言,所有内容都匹配。我不确定在过去一周左右会发生什么变化,但我的研究工作缺乏成果让我不知道在哪里寻找或做些什么来纠正这种情况。如果其他人经历过类似的事情我肯定会感谢你的帮助!

0 个答案:

没有答案