简体   繁体   English

使用 azure devops 的 rest api

[英]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?我想使用 Java 从 Azure Devops 的其余 api 获取数据。但不确定如何建立连接。可能个人访问令牌会有所帮助,但如何使用代码中的令牌来建立代码和 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?如果我对您的理解正确,您正在尝试调用 azure API,而这些 API 需要授权令牌?

For example this azure API to send data into Azure queue : https://docs.microsoft.com/en-us/rest/api/servicebus/send-message-to-queue例如这个 azure API 将数据发送到 Azure 队列: 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如果我的理解是正确的,那么您需要使用任何 REST客户端HTTP 客户端调用 REST API ,并且您需要在请求标头中传递授权令牌

For calling a Rest API in java with passing header below is an example:使用下面的标头在 java 中调用 Rest API 是一个示例:

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: 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文件链接: ResApiMain.java

You can the use java client library for azure devops rest api.您可以将 java 客户端库用于 azure devops rest api。 This will take the overload of encoding your personal access token and indeed supports OAuth authentication.这将承担编码您的个人访问令牌的重载,并且确实支持 OAuth 身份验证。

It is been actively developed and used in production.它已被积极开发并用于生产。

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

Documentation - https://azure-devops-java-sdk-docs.readthedocs.io/en/latest/?badge=latest文档 - 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.该库支持大多数 API,您可以查看 github 存储库中的文档和示例文件夹,以了解如何充分利用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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