2013-01-05 52 views
0

我正嘗試使用Apache Amber接收用於條帶連接的身份驗證令牌。還有就是如何交換的OAuth代碼的訪問令牌here一個例子:Apache Amber:如何使用承載頭交換訪問令牌的OAuth代碼?

然而,條紋需要額外的「授權:承載」頭:

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(); 

但它崩潰:

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

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

回答

0

答案被提供由Pinak沙阿這裏: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); 

謝謝,Pinak!

+0

請問您是否可以更正該URL,因爲它無法訪問? – Prateek

0

只要我能看到你正在通過無記名令牌一步到早... 你確實還在token endpoint phase

+0

那麼我應該什麼時候通過持票人令牌,以及如何做到這一點? –

+0

一旦從token enpoint獲得訪問令牌,您應該在後續階段[0]傳遞承載令牌。 [0] http://tools.ietf.org/html/rfc6749#section-7.1 –

0

大概最容易做的事情是讓上面的代碼片段擺脫靠背部分的,一旦你有訪問令牌

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

相關問題