Cannot retrieve my Access Token

Okay, I experimented in what you are trying to do, but I was also getting the same failure when doing a Form POST as well. Using cURL I was able to get the Access / Refresh Tokens as normal.

Here is a copy of the code I used. First part is the Authorization using a Form Element. Second part uses cURL to process the Access Code and get the Tokens. Once you have authorized, it will go back to the script, do the cURL and return the Tokens.

Fill in the variables at the top for your application. You will need to deal in storing the Tokens, as the Access Token lasts for 24 hours, and the Refresh Token lasts for 6 months. You will need to refresh the Tokens after 24 hours. Some developers use Cron Jobs to refresh the tokens every 21 hours, or whatever you feel.

<?php
    
    $client_id = "XXX";           // YOUR CLIENT ID 
    $client_secret = "XXX";    // YOUR CLIENT SECRET
    $redirect_uri = "XXX";      // YOUR REDIRECT URL

?>
<html>
    <head>
        
    </head>
    
    <body>
        <h1>(1). Authorize</h1>
        
        <form action="https://signin.infusionsoft.com/app/oauth/authorize" method="GET">

            <input type="text" name="client_id" id="client_id" value="<?php echo $client_id; ?>">          
            <input type="text" name="response_type" value="code">            
            <input type="text" name="scope" value="full"> 
            <input type="text" name="redirect_uri" id="redirect_uri" value="<?php echo $redirect_uri; ?>">
                        
            <input type="submit" name="submit" id="submit" value="Submit">
        </form>
                       
        
        <h1>(2). Tokens</h1>
        
        <?php
        
            if(isset($_GET['code']))
            {

                // Setup the HTTP Header Content Type.
               $headers = array('Content-Type: application/x-www-form-urlencoded');               

               // Setup the Fields we are going to post.                
               $fields = array(
                   'client_id'     => $client_id,
                   'client_secret' => $client_secret,
                   'code'          => $_GET['code'],
                   'grant_type'    => 'authorization_code',
                   'redirect_uri'  => $redirect_uri
               );

               // 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,     $headers);
               curl_setopt($ch, CURLOPT_POST,           count($fields));
               curl_setopt($ch, CURLOPT_POSTFIELDS,     http_build_query($fields));
               curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

               // Execute cURL and get the result back.
               $result = curl_exec($ch);

               // Close the cURL connection.
               curl_close($ch);

               // Get and store the Tokens.
               $response = json_decode($result);   
            
                print_r($response);
            
            }
        
        ?>
        
    </body>
</html>
2 Likes