Any real-world perl examples of oauth2 usage?

I have found that xml-rpc is getting so throttled that It’s essentially totally failing every night. So I was told to use Oauth2 method which has higher throttling limits. I’ve tried Net::OAuth2 and LWP::Authen::OAuth2 and after 38 hours of failed attempts, it’s time i ask for help. Is there anyone here using perl and using oauth2 to authenticate and then submit some API call successfully? For example, just a simple fetch a list of tags for contact 12345 ? I must be missing a step because i simply can not get any response from Keap’s system. Everything fails. Any real world users of perl that are doing routine API work with Keap using oauth2 method of authentication? I have the client id, secret, and all the info needed to successfully connect.

Hi @John_Brogan ,

I doubt very much you will get a Perl Developer responding to your question. The majority of developers and answers given here will be in the PHP language, which in part looks similar to Perl.

Have you checked out this OAuth example for LWP?

The first part of the process is to create the Authorization URL, which will be something like below. The “http_build_query” function safely encodes the URL parameters.

$url = 'https://accounts.infusionsoft.com/app/oauth/authorize?’;

$url .= http_build_query(
[
    ‘client_id’     => '**** YOUR CLIENT ID ****',
    ‘response_type’ => ‘code’,
    ‘scope’         => ‘full’,
    ‘redirect_uri’  => '**** YOUR DIRECT URL ****'
]);

Once you have the Authorization URL, you need to click on it, and authorize the connection in Keap. If all goes well, Keap will redirect back to the Redirect URI specified with the “code” URL parameter set.

The next part is to do an Access Token Request with a “grant_type” of “authorization_code”. Here is an example curl request.

$fields =
[
     'client_id'     => '**** YOUR CLIENT ID ****',
     'client_secret' => '**** YOUR CLIENT SECRET ****',
     'code'          => '**** THE CODE SENT BY KEAP ****',
     'grant_type'    => 'authorization_code',
     'redirect_uri'  => '**** YOUR DIRECT URL ****'
];

// Setup cURL so that we can post the Authorization information.
$ch = curl_init();   
            
curl_setopt($ch, CURLOPT_URL,            'https://api.infusionsoft.com/token');        
curl_setopt($ch, CURLOPT_HTTPHEADER,     [ 'Content-Type: application/x-www-form-urlencoded' ]);
curl_setopt($ch, CURLOPT_POST,           count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS,     http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    
$result = curl_exec($ch);

Keap will respond back with the Access and Refresh Tokens in which you need to store in a database.

Then every 1 hour you need to renew the Access Token using the “grant_type” of “refresh_token”. You will need to create a Cron Job to run your script.


AI Response

Using AI, it came up with the following response below.

To set up an OAuth connection to the Keap API using Perl, follow these essential steps that apply OAuth 2.0 standards, which you can implement using generic Perl modules for HTTP and OAuth or any suitable OAuth2-supporting library.
Steps to Implement OAuth for Keap in Perl
Prerequisites
• Register and create your app in the Keap (Infusionsoft) Developer Portal to get your Client ID and Client Secret.
• Set up a Redirect URI for your app.
Core OAuth 2.0 Flow
• Direct the user to Keap Authorization URL: Construct a URL like:

https://accounts.infusionsoft.com/app/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&response_type=code&scope=full
•	Redirect your user to this URL.
•	User grants access: The user logs in to Keap and authorizes your app. Keap redirects the user to your redirect URI with a `code` parameter.
•	Exchange authorization code for tokens: Use Perl to send a POST request to `https://api.infusionsoft.com/token` with:
grant_type=authorization_code
code=THE_CODE_YOU_RECEIVED
client_id=YOUR_CLIENT_ID
client_secret=YOUR_CLIENT_SECRET
redirect_uri=YOUR_REDIRECT_URI
The `Content-Type` must be `application/x-www-form-urlencoded`.
•	Store the access_token and refresh_token: Use the received access token for subsequent API requests. Use the `refresh_token` after access token expiry.

Example Perl Snippet
Below is a Perl pseudocode example (you may use LWP::UserAgent or HTTP::Tiny for HTTP requests, and URI::Escape for URL encoding):

use LWP::UserAgent;
use HTTP::Request;
use URI::Escape;

# Step 1: Redirect user:
my $auth_url = "https://accounts.infusionsoft.com/app/oauth/authorize?" .
    "client_id=$client_id&" .
    "redirect_uri=" . uri_escape($redirect_uri) . "&" .
    "response_type=code&" .
    "scope=full";
# Redirect user to $auth_url manually

# Step 2: After user authorizes and you get $code
my $ua = LWP::UserAgent->new;
my $res = $ua->post('https://api.infusionsoft.com/token', {
    grant_type    => 'authorization_code',
    code          => $code,
    client_id     => $client_id,
    client_secret => $client_secret,
    redirect_uri  => $redirect_uri
}, 'Content-Type' => 'application/x-www-form-urlencoded');

my $response = $res->decoded_content;
# Parse $response for access_token and refresh_token

You can refresh the access token by POSTing to the same /token endpoint with grant_type=refresh_token and your refresh token.
Perl OAuth2 Modules
There is no official Perl Keap SDK, but modules like LWP::UserAgent for HTTP and Net::OAuth2::Profile::WebServer can help handle the OAuth flow (though Net::OAuth is primarily OAuth 1.0). For full OAuth2 features, use generic HTTP and JSON modules in Perl.
Key Points
• Client ID/Secret are obtained from Keap Developer Portal.
• Use OAuth2 standard endpoints for user consent and token retrieval.
• Use Perl’s HTTP and JSON modules to handle the flow and store tokens.
For full, robust implementation, always refer to the official Keap OAuth2 documentation.


Hope that helps.