joan_s/azure-devops-api

Azure DevOps API client

dev-master 2020-02-27 09:09 UTC

This package is auto-updated.

Last update: 2024-11-27 19:23:23 UTC


README

A simple PHP wrapper for Azure DevOps API.

Install

Use Composer to install the library.

php composer.phar require joan_s/azure-devops-api

Running unit tests

You can run unit test to make sure the library will work properly.

vendor/bin/phpunit vendor/joan_s/azure-devops-api

Usage

Authorize access with OAuth 2.0

Before getting started you will need to register you app and get an app ID from Azure DevOps.

Then your users will need to authorize your app on their accounts. You will get an authorization code from that process, used to obtain an access token which will allow you to send request to the API.

You can follow the Azure DevOps OAuth documentation on how to obtain an authorization code.

Getting an access token

Once you have an authorization code for your app, we provide an OAuth Client to obtain the access token you will need to authenticate your requests to the API.

...

$oAuthClient = new AzureDevOps\OAuthClient('app_client_secret', 'app_redirect_uri');

$response = $oAuthClient->getOAuthTokens('authorization_code');

$accessToken = $response['access_token'];
$refreshToken = $response['refresh_token'];

You should persist the refresh token to be able to get a new access token after it expire.

Refresh access token

Refreshing the access token is the same as getting one. Use the refresh token instead of the authorization code.

...

$response = $oAuthClient->getOAuthTokens('refresh_token');

$newAccessToken = $response['access_token'];
$newRefreshToken = $response['refresh_token'];

Use access token

...

// Instantiate a client.
$client = new AzureDevOps\Client('access_token', 'https://dev.azure.com/organization');

// Get the first project of your organization.
$projects = $client->core->projects->list(['$top' => 1]);

// Extract project id from the response.
$projectId = $projects['value'][0]['id'];

// Get all workItems for this project, expanding all their fields.
$workItems = $client->workItemTracking->workItems->list($projectId, ['$expand' => 'fields']);

For more information about available endpoints, see the Azure DevOps API documentation.

Thanks