Google Oauth2 exchange code for token returning 400

I am trying to follow https://developers.google.com/identity/protocols/oauth2/web-server#exchange-authorization-code to exchange a code for an access token. I am able to get my local endpoint to trigger and in the controller I run the following. The issue is that I am getting a 400 from ‘https://oauth2.googleapis.com/token’ endpoint. I do not see a response body, which is where would usually look for the error message. Does anyone know how I can debug or what the issue might be? Note that I have ‘http://localhost:8080/oauthtokengmail’ as one of my ‘Authorized redirect URIs’, and that the redirect is working, so I do not think the issue is there.

I was successfully able to use curl to get the values … so I know it must be something in the java code.

I am executing the following code:

private String getTokenJson(String code, boolean isProduction) {

        RequestConfig requestConfig = getRequestConfig();
        BasicHttpClientConnectionManager cm = getConnectionManager();

        HttpPost post = new HttpPost("https://oauth2.googleapis.com/token");

        List<NameValuePair> pairs = new ArrayList<>();
        pairs.add(new BasicNameValuePair("code", code));
        pairs.add(new BasicNameValuePair("client_id", gmailSecretsService.getClientId()));
        pairs.add(new BasicNameValuePair("client_secret", gmailSecretsService.getClientSecret()));
        pairs.add(new BasicNameValuePair("grant_type", "authorization_code"));
        pairs.add(new BasicNameValuePair("redirect_uri", getRedirectUri(isProduction)));

        post.setEntity(new UrlEncodedFormEntity(pairs));

        //////////////////////
        try (CloseableHttpClient httpclient = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(cm)
                .build()) {
            try (CloseableHttpResponse response = httpclient.execute(post)) {
                // Get status code
                if (response.getCode() != 200) {
                    LOGGER.info("Seeing response.code()={}", response.getCode());
                    return null;
                }
                HttpEntity entity = response.getEntity();
                // Get response information
                String resultContent = EntityUtils.toString(entity);
                LOGGER.info("Seeing result={}", resultContent);
                return resultContent;
            }

        } catch (IOException | ParseException e) {
            LOGGER.info("Seeing exception", e);
            return null;
        }
    }

private static BasicHttpClientConnectionManager getConnectionManager() {
        ConnectionConfig connConfig = ConnectionConfig.custom()
                .setConnectTimeout(15000, TimeUnit.MILLISECONDS)
                .setSocketTimeout(15000, TimeUnit.MILLISECONDS)
                .build();
        BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager();
        cm.setConnectionConfig(connConfig);
        return cm;
    }

    private static RequestConfig getRequestConfig() {
        return RequestConfig.custom()
                .setConnectionRequestTimeout(Timeout.ofMilliseconds(15000))
                .build();
    }


private static String getRedirectUri(boolean isProduction) {
        if (isProduction) {
            return "https://example.com/oauthtokengmail";
        } else {
            return "http://localhost:8080/oauthtokengmail";
        }
    }

The issue was that the redirect_uri was mismatched between production and local. When the prod server used the non-localhost version, that (naturally) did not match the localhost version. Otherwise, the code works perfectly.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật