SSL/HTTPS for Google OAuth 2.0 using Java-Client

Viewed 95

I have been trying for hours to find a way to use SSL i.e. HTTPs for the callback for OAuth 2.0 Google. Java is used and my first idea to go via an Apache2 SSL proxy unfortunately didn't work.

Are there any official approaches to implement HTTPs for this? I currently use these methods, only slightly modified compared to the examples from Google:

`public Credential authorize(final NetHttpTransport httpTransport) throws IOException { // Credentials datastore FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File("datastore"));

    // Load client secrets.
    InputStream in = new FileInputStream(CLIENT_SECRETS);
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(Objects.requireNonNull(in)));

    // Build flow and trigger user authorization request.
    flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(fileDataStoreFactory)
            .setAccessType("offline")
            .setApprovalPrompt("force")
            .build();

    //System.setProperty("https.proxyHost", "myhost");
    //System.setProperty("https.proxyPort", "443");
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver.Builder().setHost("myhost").setPort(1480).build()).authorize("user");
}

public YouTube getService() throws GeneralSecurityException, IOException {
    final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    Credential credential = authorize(httpTransport);
    datastore.createGoogleOauth2RefreshToken(credential.getRefreshToken());

    logger.info(YouTubeUtils.class, "Getting new access-token using refresh-token: " + datastore.getGoogleOauth2RefreshToken().substring(0, 6));
    TokenResponse tokenResponse = new TokenResponse().setRefreshToken(datastore.getGoogleOauth2RefreshToken());
    credential = flow.createAndStoreCredential(tokenResponse, "user");
    credential.refreshToken();

    logger.info(YouTubeUtils.class, "New access-token: " + credential.getAccessToken().substring(0, 16));

    return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

`

Tried to use Apache2 SSL Proxy. Client returns callback URL mismatched Also tried to use this header options:

RequestHeader set X-Forwarded-Proto https RequestHeader set X-Forwarded-Port 443 RequestHeader set X-Forwarded-Host myhost ProxyPreserveHost On

0 Answers
Related