Back

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

Aug 16, 20245 minute read

Hey there, fellow JavaScript devs! Ready to dive into the world of Kintone integrations? Today, we're going to tackle one of the most crucial aspects of building a public integration: the authorization flow. Buckle up, because we're about to make your Kintone integration secure and user-friendly in no time!

Prerequisites

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

  • A Kintone developer account (if you don't have one, go grab it!)
  • A solid grasp on OAuth 2.0 (you know, the cool kid of auth protocols)
  • Node.js and npm installed on your machine

Setting up the project

Let's kick things off by setting up our project:

mkdir kintone-integration cd kintone-integration npm init -y npm install express axios dotenv

Configuring Kintone OAuth

First things first, let's get our Kintone app registered:

  1. Head over to your Kintone developer portal
  2. Create a new app and jot down your client ID and client secret
  3. Set up your redirect URI (something like http://localhost:3000/callback)

Implementing the authorization flow

Now for the fun part! Let's build our auth flow:

require('dotenv').config(); const express = require('express'); const axios = require('axios'); const app = express(); app.get('/login', (req, res) => { const authUrl = `https://{your-domain}.kintone.com/oauth2/authorization?client_id=${process.env.CLIENT_ID}&redirect_uri=${encodeURIComponent(process.env.REDIRECT_URI)}&response_type=code`; res.redirect(authUrl); }); app.get('/callback', async (req, res) => { const { code } = req.query; try { const tokenResponse = await axios.post('https://{your-domain}.kintone.com/oauth2/token', { grant_type: 'authorization_code', code, client_id: process.env.CLIENT_ID, client_secret: process.env.CLIENT_SECRET, redirect_uri: process.env.REDIRECT_URI }); // Store tokenResponse.data.access_token securely res.send('Authorization successful!'); } catch (error) { res.status(500).send('Authorization failed'); } }); app.listen(3000, () => console.log('Server running on port 3000'));

Building the frontend

Keep it simple, folks! A login button is all we need:

<button onclick="window.location.href='/login'">Login with Kintone</button>

Securing the backend

Always validate those tokens! Here's a quick example:

const validateToken = async (token) => { try { const response = await axios.get('https://{your-domain}.kintone.com/api/v1/users/me', { headers: { Authorization: `Bearer ${token}` } }); return response.data; } catch (error) { return null; } };

Don't forget to implement token refresh when needed!

Testing the integration

Time to put on your tester hat:

  1. Fire up your app
  2. Click that login button
  3. Go through the Kintone auth process
  4. Celebrate when you see "Authorization successful!"

Best practices and considerations

  • Always handle errors gracefully
  • Use HTTPS in production
  • Implement rate limiting to play nice with Kintone's API
  • Store tokens securely (please, no plain text storage!)

Wrapping up

And there you have it! You've just built a secure authorization flow for your Kintone integration. Pretty cool, right? Remember, this is just the beginning. From here, you can start adding more features, interacting with Kintone's API, and creating something truly awesome.

Keep coding, keep learning, and most importantly, keep having fun with Kintone integrations!