title: Groovy - Jira OAuth integration using HttpBuilder
body: <p>I want to get data using <a href="https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-oauth-authentication" rel="nofollow">JIRA REST api</a> with provided <code>JIRA OAuth authentication</code> service.</p>
  
  <p>Basically I'm able to achieve this task using <a href="https://github.com/scribejava/scribejava/wiki/getting-started" rel="nofollow"><code>ScribeJava</code></a> with <code>Groovy</code>. But I want to decoupled all the process as below :-</p>
  
  <ul>
  <li>Request to get request token</li>
  <li>Request to get authorized URL</li>
  <li>Request to access token</li>
  <li>Request to get actual data using <a href="https://github.com/jgritman/httpbuilder" rel="nofollow"><code>HTTPBuilder</code></a></li>
  </ul>
  
  <p>So I'm able to achieve above mentioned first three steps using <a href="https://github.com/scribejava/scribejava/wiki/getting-started" rel="nofollow"><code>ScribeJava</code></a> and storing the <code>accessToken</code> into <code>Database</code> for further request for data as below :-</p>
  
  <pre><code>import java.security.KeyFactory
  import java.security.PrivateKey
  import java.security.spec.PKCS8EncodedKeySpec
  
  import com.github.scribejava.core.builder.api.DefaultApi10a
  import com.github.scribejava.core.model.OAuth1RequestToken
  import com.github.scribejava.core.services.RSASha1SignatureService
  import com.github.scribejava.core.services.SignatureService
  
  
  class JiraOauthProvider extends DefaultApi10a {
  
      private String authURL
      private String requestTokenURL
      private String accessTokenURL
      private String consumerPrivateKey
  
      private JiraOauthProvider(authURL, requestTokenURL, accessTokenURL, consumerPrivateKey) {
          this.authURL = authURL
          this.requestTokenURL = requestTokenURL
          this.accessTokenURL = accessTokenURL
          this.consumerPrivateKey = consumerPrivateKey
      }
  
      private static JiraOauthProvider instance = null
  
      public static JiraOauthProvider instance(Map map) {
          if(instance == null) {
              instance = new JiraOauthProvider(map.authURL,
                      map.requestTokenURL,
                      map.accessTokenURL,
                      map.consumerPrivateKey)
          }
          return instance
      }
  
      @Override
      public String getAccessTokenEndpoint() {
          return accessTokenURL
      }
  
      @Override
      public String getRequestTokenEndpoint() {
          return requestTokenURL
      }
  
      @Override
      public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
          return String.format(authURL, requestToken.getToken())
      }
  
      @Override
      public SignatureService getSignatureService() {
          return new RSASha1SignatureService(getPrivateKey())
      }
  
      private PrivateKey getPrivateKey() {
          byte[] key = Base64.getDecoder().decode(consumerPrivateKey)
          PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key)
          KeyFactory kf = KeyFactory.getInstance("RSA")
          return kf.generatePrivate(keySpec)
      }
  </code></pre>
  
  <p>Now I'm building <code>OAuthService</code> as :-</p>
  
  <pre><code>private static final String CALLBACK_URI = "callback-url"
  protected static final String CONSUMER_KEY = "consumer-key"
  protected static final String CONSUMER_PRIVATE_KEY = "private-key"
  
  Map oAuthMap = [
                  "authURL" :"auth-url=%s",
                  "requestTokenURL":"request-token-url",
                  "accessTokenURL":"access-token-url",
                  "consumerPrivateKey":CONSUMER_PRIVATE_KEY
              ]
  
  //Buid oauth service to get request token, auth url and access token
  OAuth10aService service = ServiceBuilder()
                  .apiKey(CONSUMER_KEY)
                  .apiSecret(CONSUMER_PRIVATE_KEY).callback(CALLBACK_URI)
                  .build(JiraOauthProvider.instance(oAuthMap))
  
  OAuth1RequestToken requestToken = service.getRequestToken()
  def authURL = service.getAuthorizationUrl(requestToken)
  
  //Now after redirect to this authURL and providing credential I'm getting oauthVerifier code to get accessToken and secretToken
  
  def oauthVerifier = "oauth verifier code"
  
  //Now calling to get accessToken
  OAuth1AccessToken oAuth1AccessToken = service.getAccessToken(requestToken, oauthVerifier);
  def accessToken = oAuth1AccessToken.getToken()
  def secretToken = oAuth1AccessToken.getTokenSecret()
  //now I'm storing this `accessToken`and `secretToken` into DB for further future data request.
  </code></pre>
  
  <p>So after all above stuff I'm able to achieve above mentioned three steps and storing the access token into <code>db</code> for future request only for <code>data</code>.</p>
  
  <p>So to achieve 4th step to getting actual data using <a href="https://github.com/jgritman/httpbuilder" rel="nofollow"><code>HTTPBuilder</code></a> I'm doing some thing as below :-</p>
  
  <pre><code>def http  = new HTTPBuilder('base-url')
  
  http.auth.oauth CONSUMER_KEY, CONSUMER_PRIVATE_KEY, accessToken, secretToken
  
  http.request(Method.GET, ContentType.JSON) { req -&gt;
              uri.path = 'path'
              response.success = { resp, json -&gt;
                  println json
              }
              response.failure = { resp, json -&gt; print json }
          }
      }
  </code></pre>
  
  <p>But I'm getting response as :-</p>
  
  <pre><code>{oauth_problem=signature_method_rejected}
  </code></pre>
  
  <p>So, could anyone suggest me how can I get actual data using <a href="https://github.com/jgritman/httpbuilder" rel="nofollow"><code>HTTPBuilder</code></a> with <code>OAuth</code> authentication using <code>accessToken</code> and secretToken?</p>
  
  <p><strong>Note</strong>:- I can get actual data as well using <a href="https://github.com/scribejava/scribejava/wiki/getting-started" rel="nofollow"><code>ScribeJava</code></a> Api with <a href="https://github.com/scribejava/scribejava/blob/master/scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthRequest.java" rel="nofollow"><code>OAuthRequest</code></a> but requirement is to get actual data using <a href="https://github.com/jgritman/httpbuilder" rel="nofollow"><code>HTTPBuilder</code></a></p>
  
  <p>I just want a pointer that how to achieve it.</p>