Back

How to build a public CompanyCam integration: Building the Auth Flow

Aug 15, 20247 minute read

Hey there, fellow JavaScript enthusiast! Ready to dive into the world of CompanyCam integrations? Today, we're going to walk through building the authorization flow for a user-facing integration. Buckle up, because we're about to make your app a whole lot cooler with CompanyCam's powerful features.

Introduction

CompanyCam is a game-changer for construction and field service companies, allowing them to capture, share, and organize project photos. By building an integration, you're opening up a world of possibilities for your users. In this guide, we'll focus on the crucial first step: setting up the auth flow.

Prerequisites

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

  • A CompanyCam Developer account (if you don't have one, go grab it!)
  • A solid grasp of OAuth 2.0 (don't worry, we'll refresh your memory)
  • Node.js and Express.js ready to roll

Setting up the CompanyCam Application

First things first, let's get your app registered with CompanyCam:

  1. Head over to the CompanyCam Developer Portal
  2. Create a new application (give it a snazzy name!)
  3. Jot down your client ID and client secret (guard these with your life)
  4. Set up your redirect URI (this is where CompanyCam will send your users after they authorize your app)

Implementing the Authorization Flow

Now for the fun part – let's build this flow!

Initiating the OAuth process

const authUrl = `https://app.companycam.com/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=code`; // Redirect your user to this URL when they want to connect their CompanyCam account res.redirect(authUrl);

Handling the callback

app.get('/callback', async (req, res) => { const { code } = req.query; // Exchange the code for an access token const tokenResponse = await axios.post('https://app.companycam.com/oauth/token', { client_id: clientId, client_secret: clientSecret, code, grant_type: 'authorization_code', redirect_uri: redirectUri }); const { access_token, refresh_token } = tokenResponse.data; // Store these tokens securely (more on this in a bit) });

Storing and managing tokens

Store these tokens securely – never expose them client-side. Consider using encrypted storage or a secure database.

Don't forget to implement a token refresh mechanism:

async function refreshToken(refreshToken) { const response = await axios.post('https://app.companycam.com/oauth/token', { client_id: clientId, client_secret: clientSecret, refresh_token: refreshToken, grant_type: 'refresh_token' }); return response.data.access_token; }

Making Authenticated Requests

Now that you've got the golden ticket (aka the access token), use it to make API requests:

const response = await axios.get('https://api.companycam.com/v2/users/me', { headers: { 'Authorization': `Bearer ${accessToken}` } });

Error Handling and Edge Cases

Always be prepared! Handle expired tokens by refreshing them, and don't forget about user denials:

if (req.query.error === 'access_denied') { // Handle the case where the user denied access }

Best Practices

  • Keep your client secret... well, secret!
  • Respect CompanyCam's rate limits (they're there for a reason)
  • Always use HTTPS for secure communication

Testing the Integration

Time to put on your detective hat:

  1. Try the auth flow yourself (multiple times!)
  2. Test with expired tokens
  3. Simulate network errors

If something's not working, double-check your redirect URI and make sure your client ID and secret are correct.

Conclusion

And there you have it! You've just built the authorization flow for your CompanyCam integration. Pat yourself on the back – you're one step closer to giving your users an awesome, photo-powered experience.

Next up: start exploring CompanyCam's API endpoints and build out the rest of your integration. The sky's the limit!

Additional Resources

Now go forth and code! Your users are going to love what you build. If you hit any snags, remember: the CompanyCam developer community has got your back. Happy integrating!