获取oauth2/授权

问题描述 投票:0回答:0

我有这门课:

public class TwitterOAuth2Authorization {

    public static void main(String[] args) {
        String authorizationUrl = "https://twitter.com/i/oauth2/authorize";
        String clientId = "b2JlmUjBxVWo0NEM0c1YyMFoyVV86MTpjaQ";
        String scope = "tweet.write";
        String state = "state";
        String codeChallenge = "challenge";
        String codeChallengeMethod = "plain";

        // Construct the authorization URL with the necessary query parameters
        String fullAuthorizationUrl = authorizationUrl + "?response_type=code" +
                "&client_id=" + clientId +
                "&scope=" + scope +
                "&state=" + state +
                "&code_challenge=" + codeChallenge +
                "&code_challenge_method=" + codeChallengeMethod;

        // Simulate user clicking the link and obtaining the authorization code
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(fullAuthorizationUrl);

            // Execute the request and get the response
            HttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                // If successful, get the authorization code from the response URL
                String responseUrl = response.getFirstHeader("location").getValue();
                String authorizationCode = getAuthorizationCodeFromUrl(responseUrl);

                System.out.println("Authorization Code: " + authorizationCode);
            } else {
                System.err.println("Failed to get the authorization code. HTTP Status: " + statusCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Helper method to extract the authorization code from the response URL
    private static String getAuthorizationCodeFromUrl(String responseUrl) {
        String[] queryParameters = responseUrl.split("\\?");
        if (queryParameters.length > 1) {
            String[] params = queryParameters[1].split("&");
            for (String param : params) {
                String[] keyValue = param.split("=");
                if (keyValue.length == 2 && "code".equals(keyValue[0])) {
                    return keyValue[1];
                }
            }
        }
        return null;
    }

}

但是我有这个错误:

   org.apache.http.client.ClientProtocolException
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
        at com.pedro.varela.service.TwitterOAuth2Authorization.main(TwitterOAuth2Authorization.java:34)
    Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'https://twitter.com/i/flow/login?redirect_after_login=%2Fi%2Foauth2%2Fauthorize%3Fresponse_type%3Dcode%26client_id%3Db2JOUjBxrfVWo0NEM0c1YyMFoyVV86MTpjaQ%26scope%3Dtweet.write%26state%3Dstate%26code_challenge%3Dchallenge%26code_challenge_method%3Dplain'
        at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:176)
        at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:220)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:121)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
        ... 3 more
java spring-boot twitter oauth-2.0
© www.soinside.com 2019 - 2024. All rights reserved.