2016-10-06 9 views
0

OAuthを使用してJiraで接続を行い、接続が成功した後にJiraで問題を作成しようとしています。私は今アクセストークンを持っているので、そのアクセストークンを渡してJiraに接続する方法を知らない。OAuthを使用してJira接続を作成する

ここでここでトークン、トークン秘密、取り出さリクエストトークンURLとアクセストークン

private AtlassianOAuthClient getJiraOAuthClient() { 

     final String baseURI = "http://bmh1060149:8080"; 
     final String consumerKey = "hardcoded-consumer"; 
     final String consumerPrivatekey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFkPMZQaTqsSXI+bSI65rSVaDzic6WFA3WCZMVMi7lYXJAUdkXo4DgdfvEBO21Bno3bXIoxqS411G8S53I39yhSp7z2vcB76uQQifi0LEaklZfbTnFUXcKCyfwgKPp0tQVA+JZei6hnscbSw8qEItdc69ReZ6SK+3LHhvFUUP1nLhJDsgdPHRXSllgZzqvWAXQupGYZVANpBJuK+KAfiaVXCgA71N9xx/5XTSFi5K+e1T4HVnKAzDasAUt7Mmad+1PE+56Gpa73FLk1Ww+xaAEvss6LehjyWHM5iNswoNYzrNS2k6ZYkDnZxUlbrPDELETbz/n3YgBHGUlyrXi2PBjAgMBAAECggEAAtMctqq6meRofuQbEa4Uq5cv0uuQeZLV086VPMNX6k2nXYYODYl36T2mmNndMC5khvBYpn6Ykk/5yjBmlB2nQOMZPLFPwMZVdJ2Nhm+naJLZC0o7fje49PrN2mFsdoZeI+LHVLIrgoILpLdBAz/zTiW+RvLvMnXQU4wdp4eO6i8J/Jwh0AY8rWsAGkk1mdZDwklPZZiwR3z+DDsDwPxFs8z6cE5rWJd2c/fhAQrHwOXyrQPsGyLHTOqS3BkjtEZrKRUlfdgV76VlThwrE5pAWuO0GPyfK/XCklwcNS1a5XxCOq3uUogWRhCsqUX6pYfAVS6xzX56MGDndQVlp7U5uQKBgQDyTDwhsNTWlmr++FyYrc6liSF9NEMBNDubrfLJH1kaOp590bE8fu3BG0UlkVcueUr05e33Kx1DMSFW72lR4dht1jruWsbFp6LlT3SUtyW2kcSet3fC8gySs2r6NncsZ2XFPoxTkalKpQ1atGoBe3XIKeT8RDZtgoLztQy7/7yANQKBgQDQvSHEKS5SttoFFf4YkUh2QmNX5m7XaDlTLB/3xjnlz8NWOweK1aVysb4t2Tct/SR4ZZ/qZDBlaaj4X9h9nlxxIMoXEyX6Ilc4tyCWBXxn6HFMSa/Rrq662Vzz228cPvW2XGOQWdj7IqwKO9cXgJkI5W84YtMtYrTPLDSjhfpxNwKBgGVCoPq/iSOpN0wZhbE1KiCaP8mwlrQhHSxBtS6CkF1a1DPm97g9n6VNfUdnB1Vf0YipsxrSBOe416MaaRyUUzwMBRLqExo1pelJnIIuTG+RWeeu6zkoqUKCAxpQuttu1uRo8IJYZLTSZ9NZhNfbveyKPa2D4G9B1PJ+3rSO+ztlAoGAZNRHQEMILkpHLBfAgsuC7iUJacdUmVauAiAZXQ1yoDDo0Xl4HjcvUSTMkccQIXXbLREh2w4EVqhgR4G8yIk7bCYDmHvWZ2o5KZtD8VO7EVI1kD0z4Zx4qKcggGbp2AINnMYqDetopX7NDbB0KNUklyiEvf72tUCtyDk5QBgSrqcCgYEAnlg3ByRd/qTFz/darZi9ehT68Cq0CS7/B9YvfnF7YKTAv6J2Hd/i9jGKcc27x6IMi0vf7zrqCyTMq56omiLdu941oWfsOnwffWRBInvrUWTj6yGHOYUtg2z4xESUoFYDeWwe/vX6TugL3oXSX3Sy3KWGlJhn/OmsN2fgajHRip0="; 

     AtlassianOAuthClient jiraoAuthClient = new AtlassianOAuthClient(consumerKey, consumerPrivatekey, baseURI, ""); 

     return jiraoAuthClient; 
    } 

を得るために私のコードは

private String getAccessToken() { 

     AtlassianOAuthClient jiraoAuthClient = getJiraOAuthClient(); 
     TokenSecretVerifierHolder requestToken = jiraoAuthClient.getRequestToken(); 
     String authorizeUrl = jiraoAuthClient.getAuthorizeUrlForToken(requestToken.token); 
     String token = requestToken.token; 
     String tokenSecret = requestToken.secret; 
     System.out.println("Token is " + requestToken.token); 
     System.out.println("Token secret is " + requestToken.secret); 
     System.out.println("Retrieved request token. go to " + authorizeUrl); 

     URI uri = null; 
     try { 
      uri = new URI(authorizeUrl); 
     } catch (URISyntaxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     if (Desktop.isDesktopSupported()) { 
      Desktop desktop = Desktop.getDesktop(); 
      try { 
       desktop.browse(uri); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     String accessToken = jiraoAuthClient.swapRequestTokenForAccessToken(token, tokenSecret, ""); 
     // String verifier = requestToken.verifier; 
     System.out.println("Access token is : " + accessToken); 
     return accessToken; 
    } 

アクセストークンを取得するための方法であるこれらのメソッドは、私を与えている

Token is 38ESi9IJW5u3vKDslPFtuV1ZtzDpr6zi 
Token secret is cnDSL8oJyuoaQdRcFDwgHzLppSshQn9b 
Retrieved request token. go to http://bmh1060149:8080/plugins/servlet/oauth/authorize?oauth_token=38ESi9IJW5u3vKDslPFtuV1ZtzDpr6zi 
Access token is : 015CeJiH8cpI5R3OKpNco158kApq8YwV 

ここでは、これらを使用してジラ接続を作成します。以前はJira接続を作成するために基本認証を使用していましたが、今はOauthを使用してJira接続を作成します。ここでは、Basic認証を使用してJira接続を行うためのコードを示します。誰でも私にコードを微調整する方法を教えてください。そうでない場合は、OAuthを使用してJira接続を作成できる他の方法を提供できますか?

public String jiraConnectionpost(String auth, String url, String key) throws JSONException { 

     System.out.println("Inside jiraConnectionpost for JiraAdapterImpl"); 

     WebResource webResource = client.resource(url); 
     ClientResponse response = webResource.header("Authorization", "Basic " + auth).type("application/json") 
       .accept("application/json").post(ClientResponse.class, key); 
     int statusCode = response.getStatus(); 

     if (statusCode == 401) { 
      try { 
       throw new AuthenticationException("Invalid Username or Password"); 
      } catch (AuthenticationException e) { 
       e.printStackTrace(); 
      } 
     } 
     String jsonString = response.getEntity(String.class); 

     url = null; 
     return jsonString; 
    } 

答えて

0

3日間の研究私は最終的に、OAuthを使用してJiraで問題を作成、更新、削除するソリューションにクラックを入れました。 OAuthを使用してJiraで作成操作を行う場合、makeAuthenticatedRequestは(String url、String accessToken、InputStream bodyAsStream、String operationType)のようにパラメータを取るため、入力ストリームの形式としてjsonデータを渡します。詳細については、以下のコードを参照してください。

public void createJiraIssue(JiraExecutionDTO jiraEx) { 

     System.out.println("Inside createJiraIssue for JiraAdapterImpl"); 

     String jiraUrl = jiraEx.getJiraUrl(); 

     String operationType = "CREATE"; 

     String jSonData = "Your Json data for create operation"; 
     try { 
      InputStream inputStream = new ByteArrayInputStream(jSonData.getBytes("UTF-8")); 
      OAuthMessage response = getResponseForRequest(jiraUrl, inputStream, operationType); 
      System.out.println(response); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

private OAuthMessage getResponseForRequest(String url, InputStream inputstream, String operationType) { 

     AtlassianOAuthClientRequest jiraOauthClient = getJiraOAuthClient(); 
     String accessToken = getAccessToken(); 
     OAuthMessage response = null; 
     try { 
      response = jiraOauthClient.makeAuthenticatedRequest(url, accessToken, inputstream, operationType); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return response; 
    } 

public OAuthMessage makeAuthenticatedRequest(String url, String accessToken, InputStream bodyAsStream, 
      String operationType) throws Exception { 

     //boolean suppressNPE = false; 
     String method = null; 

     OAuthAccessor accessor = getAccessor(); 
     OAuthClient client = new OAuthClient(new HttpClient4()); 
     accessor.accessToken = accessToken; 
     OAuthMessage request = null; 
     if (operationType == "CREATE") { 
      method = OAuthMessage.POST; 
      request = accessor.newRequestMessage(method, url, Collections.<Map.Entry<?, ?>> emptySet(), bodyAsStream); 
     } else if (operationType == "UPDATE") { 
      method = OAuthMessage.PUT; 
      request = accessor.newRequestMessage(method, url, Collections.<Map.Entry<?, ?>> emptySet(), bodyAsStream); 
     } else if (operationType == "DELETE") { 
      method = OAuthMessage.DELETE; 
      request = accessor.newRequestMessage(method, url, Collections.<Map.Entry<?, ?>> emptySet()); 
     } 
     List<Map.Entry<String, String>> headers = request.getHeaders(); 
     headers.add(new OAuth.Parameter(HttpMessage.CONTENT_TYPE, "application/json")); 
     // headers.add(new OAuth.Parameter(HttpMessage.CONTENT_LENGTH, 
     // contentLength)); 
     OAuthMessage response = client.invoke(request, ParameterStyle.QUERY_STRING); 

     return response; 

    } 

すべての取得操作では、makeAuthenticatedRequestメソッドに変更があります。

public String makeAuthenticatedRequest(String url, String accessToken) { 
     try { 
      OAuthAccessor accessor = getAccessor(); 
      OAuthClient client = new OAuthClient(new HttpClient4()); 
      accessor.accessToken = accessToken; 
      OAuthMessage response = client.invoke(accessor, url, Collections.<Map.Entry<?, ?>> emptySet()); 
      return response.readBodyAsString(); 
     } catch (Exception e) { 
      throw new RuntimeException("Failed to make an authenticated request.", e); 
     } 
    } 

public void getAllIssueTypes(JiraExecutionDTO jcqcred) { 

     System.out.println("Inside getAllIssuetypeAssociatedToProject for JiraAdapterImpl"); 

     String jiraURL = jcqcred.getJiraUrl(); 
     if (!jiraURL.endsWith("/")) { 
      jiraURL = jiraURL + "/"; 
     } 

     String url = jiraURL + "rest/api/2" + "/" + "issuetype"; 

     String issueTypes = getResponseForRequest(url); 

     System.out.println("Issuetype associated to project are\n" + issueTypes); 
    } 

さらに詳しくは、rest-oauth-client-1.0.one-jarを参照してください。このjarをjava decompilerで開き、クラスAtlassianOAuthClient.class、JIRAOAuthClient.class、TokenSecretVerifierHolder.classを参照してください。これにより、上記の方法をどのように実装したかを完全に理解することができます。

+0

私はoAuthを使用して接続を作成しようとしています。私はアクセサーをラインでヌルにしています OAuthAccessor accessor = getAccessor(); JIRAアクセサーに何が間違っているのかを教えてください。 –

+0

こんにちはBindu ....あなたgetAccessor()メソッド内に移動し、oauth接続で何が間違っているかを知るためにそれをデバッグする必要があります –

関連する問題