使用OAuth身份验证将访问令牌传递给Jira

时间:2016-09-29 07:22:41

标签: oauth jira jira-rest-java-api

我在Jira中使用OAuth身份验证来使用JIRA Rest Java Client在jira中测试一些方法。我使用OAuth身份验证获取了访问令牌,我需要传递Jira URL。以下是我获得访问令牌的所有内容。

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 URL,但我得到一个空数组。请让我知道我在哪里做错了,或者我需要将哪些更改合并到我的代码中才能使这件事工作。这是我的代码。

public void getAllIssueTypesUsingOAuth(JiraCQCredential jcqcred) {

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

        //String username = jcqcred.getUserName();
        //String password = jcqcred.getPassword();
        String jiraURL = jcqcred.getJiraUrl();
        if (!jiraURL.endsWith("/")) {
            jiraURL = jiraURL + "/";
        }
        try {
            String accessToken = JiraAdapterImpl.getAccessToken(); // This method is giving me access token
            URL url = new URL(jiraURL + "rest/api/2" + "/" + "issuetype?access_token=" + accessToken);
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
            httpConnection.setRequestProperty("Content-Type", "application/json");
            httpConnection.setRequestMethod("GET");
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            String issueTypes = sb.toString();
            System.out.println("Issuetype associated to project are\n" + issueTypes);
            JSONArray jsonArray = new JSONArray(issueTypes);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String issueNames = (String) jsonObject.get("name");
                System.out.println(issueNames);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

它给了我一个像[]

这样的空数组

1 个答案:

答案 0 :(得分:0)

嗨搜索了一下后,我终于找到了解决上述问题的方法。获取访问令牌后,只需将该访问令牌传递给makeAuthenticatedRequest(url,accessToken)方法,该方法将为您提供要检索的结果数据。这里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);

        String accessToken = jiraoAuthClient.swapRequestTokenForAccessToken(token, tokenSecret, "");
        System.out.println("Access token is : " + accessToken);
        return accessToken;
    }

这是您调用以检索数据的方法。

public void getAllCommentOfIssueUsingOAuth() {

        logger.info("Inside getAllCommentOfIssue for JiraAdapterImpl");

        AtlassianOAuthClient jiraoAuthClient = getJiraOAuthClient();
        String accessToken = getAccessToken();
        String url = "your Jira URL";
        String responseAsString = jiraoAuthClient.makeAuthenticatedRequest(url, accessToken);
        System.out.println(responseAsString);
    }

这将在resulString中为您提供结果JSON数据或XML数据。