This is my first post so I am sorry if the formatting is off. Basically I am trying to get acclimated with the api for infusionsoft so that I can build an application. Right now I’m just pulling some basic data from contacts but my code only works the first time I authorize. To be comprehensive, what is happening is I reach the website and it asks me to authorize. I click the link, sign in, and authorize the application with my account it then sends me back to index.php with the contact information I requested; so far so good. The problem occurs when I press refresh. I assume that it should just resend the contact information I originally requested but instead it gives me “400 bad request” error stating the authorization code is invalid. Any help would be greatly appreciated so that I can move forward. My entire code is given below. Thanks community.
<?php //if(empty(session_id())){} //session_start(); require_once 'vendor/autoload.php'; ?><!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Merriweather|Oswald" rel="stylesheet">
$infusionsoft = new \Infusionsoft\Infusionsoft(array(
'clientId' => 'xxxx',
'clientSecret' => 'xxxx',
'redirectUri' => 'http://example.com',
));
// If the serialized token is available in the session storage, we tell the SDK
// to use that token for subsequent requests.
if (isset($_SESSION['token'])) {
$infusionsoft->setToken(unserialize($_SESSION['token']));
}
// If we are returning from Infusionsoft we need to exchange the code for an
// access token.
if (isset($_GET['code']) and !$infusionsoft->getToken()) {
$infusionsoft->requestAccessToken($_GET['code']);
}
if ($infusionsoft->getToken()) {
// Save the serialized token to the current session for subsequent requests
$_SESSION['token'] = serialize($infusionsoft->getToken());
$table = "Contact";
$limit = 10;
$page = 0;
$queryData = array("Id" => "~>=~71251");
$selectedFields = array("FirstName");
$orderBy = "FirstName";
$ascending = true;
$infusionsoft->refreshAccessToken();
$contact = $infusionsoft->data()->query($table, $limit, $page, $queryData, $selectedFields, $orderBy, $ascending);
//This section loops through array assigns as (Like SQL) and goes through that array
echo "<pre>";
echo "First";
foreach ( $contact as $var ) {
echo "\n", $var['FirstName'], "\t";
}
// MAKE INFUSIONSOFT REQUEST
} else {
echo '<div class="centering"><a href="' . $infusionsoft->getAuthorizationUrl() . '" class="auth">Click here to authorize</a></div>';
}
?>