You may be able to automated that if you make the user simply go to some web page you create (once) and use a modified version of the code I posted to store auth and refresh tokens on a per-user basis rather than globally, as I do.
Has anyone got this to work in an unattended process? The only OAuth flows they provide appear to require user interaction to get the code, which completely eliminates batch jobs that run at 1AM to upload contacts and send out emails. Or am I missing something?
You only need to do the manual process once. From then on, it works without any further manual interaction.
Step 1 (manual-one time): Call the URL to get an authorization code. I have it respond to local host, which errors, but you can get the authorization code from the response URL:
Step 2 (only necessary the first time): Calls return both an authorization code and a refresh_token. Save the refresh_token in a file and use the authorization code for any API calls:
https://idfed.constantcontact.com/as/token.oauth2?code=' + self.authorization_code + '&redirect_uri=https://localhost&grant_type=authorization_code
Step 3: Get a new authorization code and refresh_token (saving to a file) once the authorization_code from step 2 expires:
https://idfed.constantcontact.com/as/token.oauth2?refresh_token=' + refresh_token + '&grant_type=refresh_token
So, I do step 1 and step 2 once.
I do step 3 every time I run my app from then on.
(Note, It's been several months since I last worked on my code, so I hope I've responded accurately).
Doesn't work unfortunately. I am able to get a authorization code / refresh token interactively using both the browser and postman. But I am unable to do it from my program.
Our program only runs once every few days, and the token retrieved would have expired long before I would be able to use it.
Unless Refresh tokens live forever?
Yes. The RefreshToken is what you use as it lives forever ... or so it seems. Simply be sure to ask for and store the new RefreshToken for use next time.
View API documentation, code samples, get your API key.Visit Page