簡體   English   中英

Apache Amber:如何與Bearer Header交換OAuth代碼以獲得訪問令牌?

[英]Apache Amber: How to exchange OAuth code for an access token with Bearer Header?

我正在嘗試使用Apache Amber接收Stripe Connect的身份驗證令牌。 還有就是如何交換的OAuth代碼的訪問令牌的例子在這里

但是,Stripe需要其他“授權:承載”標頭:

  curl -X POST https://connect.stripe.com/oauth/token \
      -H "Authorization: Bearer xxxxxxxxxxxxxx" \
      -d code=AUTHORIZATION_CODE \
      -d grant_type=authorization_code

我嘗試了以下方法:

            OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
            String error = oar.getParam("error");
            String errorDescription = oar.getParam("error_description");
            String code = oar.getCode();


            if (null != error && !error.isEmpty()){
                System.err.println ("Authentication failed: " + errorDescription);
                System.exit(1);
            }

            OAuthClientRequest exchangeRequest = OAuthClientRequest
            .tokenLocation("https://connect.stripe.com/oauth/token")
            .setGrantType(GrantType.AUTHORIZATION_CODE)
            .setClientId("my-client-id")
            .setCode(code)
            .buildBodyMessage();

            Map<String,String> headers =new HashMap<String, String>();
            headers.put("Authorization", "Bearer xxxxxxxxxxxxxx");

            exchangeRequest.setHeaders(headers);

           //create OAuth client that uses custom http client under the hood
           OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());


           GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(exchangeRequest, GitHubTokenResponse.class);

           String accessToken = oAuthResponse.getAccessToken();

但它崩潰與:

服務器返回URL的HTTP響應代碼:401: https : //connect.stripe.com/oauth/token

關於如何添加承載頭的任何想法? 謝謝!

只要我能看到您正在將Bearer Token提前一步,就可以了……您確實仍處於令牌端點階段

可能最簡單的方法是擺脫上面代碼段中的承載部分,並且一旦獲得訪問令牌

String accessToken = oAuthResponse.getAccessToken();

使用

GET /resource?access_token=mF_9.B5f-4.1JqM HTTP/1.1

至於http://tools.ietf.org/html/rfc6750#section-2.3

Pinak Shah在這里提供了答案: https : //support.stripe.com/questions/how-can-i-use-the-java-bindings-with-oauth

  OAuthClientRequest oAuthRequest = OAuthClientRequest
                .tokenLocation(
                        paymnetInfoMsgs
                                .getMessage("stripe.website.token.url"))
                .setGrantType(GrantType.AUTHORIZATION_CODE)
                .setClientId(paymnetInfoMsgs.getMessage("stripe.clientID"))
                .setParameter("Authorization",
                        paymnetInfoMsgs.getMessage("stripe.aouthorization"))
                .setCode(code).buildBodyMessage();

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", paymnetInfoMsgs
                .getMessage("stripe.aouthorization"));
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        // create OAuth client that uses custom http client under the hood
        URLConnectionClient urlConnectionClient = new URLConnectionClient();
        oAuthResponse = urlConnectionClient.execute(oAuthRequest, headers,
                "POST", OAuthJSONAccessTokenResponse.class);

謝謝,皮納克!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM