Back

How to build a public UKG Pro Recruiting integration: Building the Auth Flow

Aug 11, 20247 minute read

Hey there, fellow JavaScript enthusiast! Ready to dive into the world of UKG Pro Recruiting integrations? Today, we're going to tackle the all-important authorization flow. Buckle up, because we're about to make your integration secure and user-friendly in no time.

Introduction

UKG Pro Recruiting is a powerful tool, but its true potential shines when integrated with other systems. The key to a smooth, secure integration? A rock-solid authorization flow. Let's build one together!

Prerequisites

Before we jump in, make sure you've got:

  • UKG Pro Recruiting API credentials (if you don't have these, reach out to your UKG rep)
  • A Node.js environment with Express.js set up

Got those? Great! Let's get coding.

OAuth 2.0 Flow Overview

We'll be using the Authorization Code Grant type of OAuth 2.0. It's like a secret handshake between your app and UKG Pro Recruiting. You'll need three key pieces:

  • Client ID
  • Client Secret
  • Redirect URI

Keep these safe – they're your app's VIP pass to the UKG Pro Recruiting API.

Setting up the Authorization Request

First things first, let's construct that authorization URL:

const authUrl = 'https://ukg-pro-recruiting.com/oauth/authorize'; const params = new URLSearchParams({ client_id: YOUR_CLIENT_ID, redirect_uri: YOUR_REDIRECT_URI, response_type: 'code', scope: 'recruiting_api' }); const fullAuthUrl = `${authUrl}?${params.toString()}`;

Now, when a user hits this URL, they'll be redirected to UKG to grant permissions. Once they do, UKG will send them back to your redirect_uri with an authorization code. Catch it like this:

app.get('/callback', (req, res) => { const code = req.query.code; // We'll use this code in the next step });

Exchanging the Authorization Code for Access Token

Time to trade in that code for an access token:

const axios = require('axios'); async function getAccessToken(code) { const tokenUrl = 'https://ukg-pro-recruiting.com/oauth/token'; const params = new URLSearchParams({ grant_type: 'authorization_code', code, client_id: YOUR_CLIENT_ID, client_secret: YOUR_CLIENT_SECRET, redirect_uri: YOUR_REDIRECT_URI }); const response = await axios.post(tokenUrl, params.toString(), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); return response.data.access_token; }

Store this token securely – it's your golden ticket to the API!

Refreshing the Access Token

Access tokens don't last forever. When they expire, use the refresh token to get a new one:

async function refreshAccessToken(refreshToken) { const tokenUrl = 'https://ukg-pro-recruiting.com/oauth/token'; const params = new URLSearchParams({ grant_type: 'refresh_token', refresh_token: refreshToken, client_id: YOUR_CLIENT_ID, client_secret: YOUR_CLIENT_SECRET }); const response = await axios.post(tokenUrl, params.toString(), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); return response.data.access_token; }

Making Authenticated API Requests

Now for the fun part – using your shiny new access token:

async function getRecruitingData(accessToken) { const apiUrl = 'https://ukg-pro-recruiting.com/api/v1/some-endpoint'; const response = await axios.get(apiUrl, { headers: { 'Authorization': `Bearer ${accessToken}` } }); return response.data; }

Error Handling and Edge Cases

Always be prepared for things to go sideways. Here's a quick error handler:

function handleApiError(error) { if (error.response) { console.error('API Error:', error.response.status, error.response.data); // Handle specific error codes here } else { console.error('Network Error:', error.message); } }

Testing the Auth Flow

Time to put it all together and test:

  1. Start your server
  2. Navigate to your authorization URL
  3. Grant permissions on the UKG page
  4. Catch the code in your callback route
  5. Exchange it for an access token
  6. Make an API request

If all goes well, you should see some recruiting data. If not, check those error logs!

Conclusion

And there you have it – a secure, user-friendly authorization flow for your UKG Pro Recruiting integration. You've tackled OAuth 2.0, handled access tokens like a pro, and even prepared for the unexpected.

What's next? Start building out those awesome features for your integration. The sky's the limit!

Additional Resources

Want to dive deeper? Check out:

Happy coding, and may your integrations be ever secure and scalable!