简体   繁体   中英

Using rest api of azure devops

I want to fetch data from the rest api of Azure Devops using Java.But not sure how to establish the connection.May be personal acces token will help,but how to use the token in Code for establishing the connection between code and azure devops? An example from anyone will be very helpful.

A code example will be very helpfull

If I am understanding you correctly, you are trying to call azure APIs, and those API need authorization token?

For example this azure API to send data into Azure queue : https://docs.microsoft.com/en-us/rest/api/servicebus/send-message-to-queue

It needs some payload and Authorization in request header !!


If my Understanding is correct, than from java you need to use any rest client or HTTP client to call the REST API and you need to pass the Authorization token in the request header

For calling a Rest API in java with passing header below is an example:

MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("Authorization", "Bearer <Azure AD JWT token>"); // set your token here

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); //someother http headers you want to set

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

RestTemplate restTemplate = new RestTemplate();
String azure_url = "https://azure_url"; // your azure devops REST URL

ResponseEntity<String> response = restTemplate.postForEntity(azure_url, request , String.class);

A small example with httpclient:

static String ServiceUrl = "https://dev.azure.com/<your_org>/";
static String TeamProjectName = "your_team_project_name";
static String UrlEndGetWorkItemById = "/_apis/wit/workitems/";
static Integer WorkItemId = 1208;
static String PAT = "your_pat";

String AuthStr = ":" + PAT;
Base64 base64 = new Base64();
        
String encodedPAT = new String(base64.encode(AuthStr.getBytes()));
        
URL url = new URL(ServiceUrl + TeamProjectName + UrlEndGetWorkItemById + WorkItemId.toString());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
        
con.setRequestProperty("Authorization", "Basic " + encodedPAT);
con.setRequestMethod("GET");
        
int status = con.getResponseCode();

Link to the file: ResApiMain.java

You can the use java client library for azure devops rest api. This will take the overload of encoding your personal access token and indeed supports OAuth authentication.

It is been actively developed and used in production.

Source code - https://github.com/hkarthik7/azure-devops-java-sdk

Documentation - https://azure-devops-java-sdk-docs.readthedocs.io/en/latest/?badge=latest

A quick example:

public class Main {
    public static void main(String[] args) {
        String organisation = "myOrganisationName";
        String personalAccessToken = "accessToken";
        String projectName = "myProject";

        // Connect Azure DevOps API with organisation name and personal access token.
        var webApi = new AzDClientApi(organisation, project, personalAccessToken);

        // call the respective API with created webApi client connection object;
        var core = webApi.getCoreApi();
        var wit = webApi.getWorkItemTrackingApi();

        try {
            // get the list of projects
            core.getProjects();

            // get a workitem
            wit.getWorkItem(15);

            // Get a work item and optionally expand the field
            wit.getWorkItem(15, WorkItemExpand.ALL);
            
        } catch (AzDException e1) {
            e1.printStackTrace();
        }
    }
}

The library has support to most of the APIs and you can view the documentation and examples folder in the github repo to know how to get the most out of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM