有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Google API认证的java问题

我正在尝试运行以下示例

https://developers.google.com/identity/sign-in/web/server-side-flow#step_1_create_a_client_id_and_client_secret

在第7步之前,一切都正常运行。我得到以下例外

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "redirect_uri_mismatch",
  "error_description" : "Bad Request"
}
    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.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
    at {package}.service.SecurityService.getProfile(SecurityService.java:55)
.....

我的代码如下所示:

public Result getProfile(User auth){
        Result result = new Result();

        try {

            // Set path to the Web application client_secret_*.json file you downloaded from the
            // Google API Console: https://console.developers.google.com/apis/credentials
            // You can also find your Web application client ID and client secret from the
            // console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
            // object.

            String CLIENT_SECRET_FILE = "client_secret.json";

            GoogleClientSecrets clientSecrets = loadSecret(CLIENT_SECRET_FILE);

                    GoogleTokenResponse tokenResponse =
                    new GoogleAuthorizationCodeTokenRequest(
                            new NetHttpTransport(),
                            JacksonFactory.getDefaultInstance(),
                            "https://www.googleapis.com/oauth2/v4/token",
                            clientSecrets.getDetails().getClientId(),
                            clientSecrets.getDetails().getClientSecret(),
                            auth.getCode(),"http://localhost:8080/api/security/googleAPICallback")
                            .execute();

            String accessToken = tokenResponse.getAccessToken();

            // Use access token to call API
            //GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);

            // Get profile info from ID token
            GoogleIdToken idToken = tokenResponse.parseIdToken();
            GoogleIdToken.Payload payload = idToken.getPayload();

            auth.setAccessToken(accessToken);
            auth.setUuid(payload.getSubject()); // Use this value as a key to identify a user.
            auth.setEmail(payload.getEmail());
            auth.setVerifiedEmail(payload.getEmailVerified());
            auth.setName(String.valueOf(payload.get("name")));
            auth.setPictureURL(String.valueOf(payload.get("picture")));
            auth.setLocale(String.valueOf(payload.get("locale")));
            auth.setFamilyName(String.valueOf(payload.get("family_name")));
            auth.setGivenName(String.valueOf(payload.get("given_name")));

            HashMap<String,Object> map = new HashMap<>();
            Field[] fields = auth.getClass().getDeclaredFields();
            for(Field field : fields){
                field.setAccessible(true);
                map.put(field.getName(), PropertyUtils.getSimpleProperty(field.getName(), field.getName()));
            }

            logger.info(auth.toString());

            result.setCode(Result.OK);
            result.setMessage("¡Exito!");
            result.setVarious(false);
            result.setData(map);

        }catch (Exception e){
            e.printStackTrace();
            result.setCode(Result.BAD_REQUEST);
            result.setMessage("¡No hay access_token!");
            result.setVarious(false);
        }

        return result;
    }

我已经尝试从本地服务器和生产服务器添加不同的端点。这两个链接都接受GET和POST方法,并返回一个“OK”json响应。另外,这两个链接已经以授权URI重定向形式添加到Google控制台中

如果我留下一个空字符串,它抛出并错误地说它需要一个重定向uri,我省略了我抛出的空格并错误地说它缺少令牌的方案

额外:

每次我在谷歌控制台中更改某些内容时,我都会重新下载我的客户端密码。json


共 (0) 个答案