OAuth#

Prerequisites#

See QuickStart

Examples#

OAuthApi offers a thin wrapper around the existing functionality. All the available methods are static and it offers a functionality that refreshes the token automatically if it expires.

Note

You should register your application for using OAuth authentication. Follow the documentation to know how to register your application.

Once you've registered your application you can then use the functionalities that azd offers.

Available functions#

Get Authorization endpoint#

Get the authorization endpoint for your application with client id, state, scope and redirect url.

// An example from the documentation

String authorizationEndpoint =  OAuthApi.getAuthorizationEndpoint(
    "88e2dd5f-4e34-45c6-a75d-524eb2a0399e", 
    "User1", 
    "vso.work%20vso.code_write", 
    "https://fabrikam.azurewebsites.net/myapp/oauth-callback");

Get the access token#

Get the access token. This method returns AuthorizedToken object and automatically injects the time stamp to the result.

OAuthApi.getAccessToken("appSecret", "authorization Code", "call-back url");

Get the refresh token#

Get the refresh token using app secret, authorization code and call back url.

OAuthApi.getRefreshToken("appSecret", "authorization Code", "call-back url");

// You can call the helper method to check if the token has expired

OAuthApi.hasTokenExpired(AuthorizedToken); // Authorized token object

Tip

You can create a connection object using Connection Api and set the defaults along with Authorization object, app secret, call back url. Then call getPersonalAccessToken method from it which returns the OAuth token and refreshes the token if it has expired and returns the new token.