Hi,
First, I am new to OAuth and am pretty sure it is the wrong tool for what I’m trying to do. With that said, I may be missing something obvious to someone just because I’m just learning this. With that said, I have been going through here and the FB group trying to figure this out.
I’m trying to add a contact and then add a tag. I thought I had this figured out using the OAuth API when I then figured out that there isn’t a client_credential grant. To give some context I am aiming to do the following on my Node.js server:
- Get a text from Twilio
- Create a contact using a phone number, a zip code, a tag, and a custom field.
- If a user texts in STOP: Search for user by phone, remove tag.
- If a user texts in START: Search for user by phone, add tag.
I almost have this worked out, but am seeing that the auth token that I receive is for 24 hours.
With a client_credential I think I know how to handle refreshing this programmatically, but I am not sure with how this is working. From reading around I think I could maybe:
- Manually get the Auth Token
- Set a timer for 20 hours and try to refresh (as was suggested here: Using OAuth2 and Infusionsoft API/REST - YouTube)
- Assuming the refresh works, I never have to manually retrieve the Auth Token again?
Thoughts? Will I need to attach a database to my server for this to work?
ALSO
I could maybe get by if I could just add the user using the API Passphrase & Encrypted Key since I don’t believe OAuth is really designed for my purposes. Does anyone know if it is possible at this point to do an HTTP post with just those? I see that a number of 3rd party apps just ask for those two items to integrate, so I’m assuming it is, but didn’t find (or recognize?) an example.
Thanks in advance.
UPDATE on 20190318:
This entire process was very painful for me, so in hopes of helping someone out, I’m going to note some things:
-
I am not using this for user authentication (letting people sign into something using InfusionSoft authentication). I am just using it to create a customer, from my server, with some custom fields, and then add a tag to that user (you cannot at this time add a tag while creating a user)
-
As crazy as this may or may not sound, the authentication to use this REST api has to be refreshed every 24 hours using your refresh token
-
the refresh token expires every 6 months OR when you get a new access token, which will likely be every 24 hours. Getting a new access token will also provide you a new refresh token).
-
You can either schedule to refresh your access and refresh tokens prior to that 24 hour mark (best practice) or you could just wait for it to fail and then refresh it then (what I’m currently doing, I will change soon).
Here is some code I am using in a Node.js and Express app that creates a contact using the REST API:
const express = require('express');
const request = require('request');
function keapCreateContact(keapZip, keapPhone, keapURL, keapMarketInfo) {
// the below tokens are expired. best practice is to not hard code this
var accessToken = '9frcdjdywh9zmbp87dqm4zu5';
var refreshToken = 'jzhtn9whzvzgzmbyu9hc8ypb';
var rootUrl = 'https://api.infusionsoft.com/crm/rest/v1/';
var authToken = 'Bearer ' + accessToken;
var createContact = {
method: 'PUT',
url: rootUrl + '/contacts',
headers:
{
Authorization: authToken,
'Content-Type': 'application/json'
},
body:
{
addresses: [{ zip_code: keapZip, field: 'BILLING' }],
phone_numbers: [{ field: 'PHONE1', number: keapPhone }],
custom_fields: [{ content: keapURL, id: 183 }, { content: keapMarketInfo, id: 177 }],
duplicate_option: 'Email',
// email_addresses: [{ email: keapEmail, field: 'EMAIL1' }]
},
json: true
};
request(createContact, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
console.log('[INFO] Contact created');
var contactId = body.id;
var addTag = {
method: 'POST',
url: rootUrl + '/contacts/' + contactId + '/tags',
headers:
{
Authorization: authToken,
'Content-Type': 'application/json'
},
body: { tagIds: [2069] }, // whatever tag(s) you want added
json: true
};
request(addTag, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
});
}
Here is the code I’m using to refresh with:
// this is working as of: (20190315•01:53:00)
var request = require('request');
var client_id = *clientIdVariable*;
var client_secret = *clientSecretVariable*;
var base64Variable = Buffer.from(client_id + ':' + client_secret).toString('base64');
console.log(base64Variable);
var options = {
method: 'POST',
url: 'https://api.infusionsoft.com/token',
headers: {
'cache-control': 'no-cache',
host: 'api.infusionsoft.com',
authorization: 'Basic ' + base64Variable,
'content-type': 'application/x-www-form-urlencoded' },
form: {
grant_type: 'refresh_token',
refresh_token : *refreshTokenVariable*,
client_id: *client_id*
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Good luck…and