0
    use QuickBooksOnline\API\DataService\DataService; 
    use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer; 
    use QuickBooksOnline\API\Facades\Customer; 
    use QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2LoginHelper;

    $oauth2LoginHelper = new OAuth2LoginHelper("clientId","clientSecret");
    $accessTokenObj = $oauth2LoginHelper->refreshAccessTokenWithRefreshToken("Q02i05iXG98OaKON8coU5fKmUCuzEkESkpkbXUcViPVnXaJ1eK");
    $accessTokenValue = $accessTokenObj->getAccessToken();
    $refreshTokenValue = $accessTokenObj->getRefreshToken();

    // Prep Data Services
    $dataService = DataService::Configure(array(
        'auth_mode' => 'oauth2',
        'ClientID' => "ClientID",
        'ClientSecret' => "ClientSecret",
        'accessTokenKey' => $accessTokenValue,
        'refreshTokenKey' => $refreshTokenValue,
        'QBORealmID' => "3644364364363463634",
        'baseUrl' => "sandbox-quickbooks.api.intuit.com"
    ));
    $dataService->setLogLocation("/Users/hlu2/Desktop/newFolderForLog");
    $dataService->throwExceptionOnError(true);
    //Add a new Vendor
    $theResourceObj = Customer::create([
        "BillAddr" => [
            "Line1" => $clientInformation['clientHomeAddress'],
            "City" => "t43",
            "Country" => "43t3",
            "CountrySubDivisionCode" => "34tt334",
            "PostalCode" => ""
        ],
        "Notes" => "3t34t",
        "Title" => "34t434t",
        "GivenName" => $clientName[0],
        "MiddleName" => "rehhhreherher",
        "FamilyName" => $clientName[1],
        "Suffix" => "Jr",
        "FullyQualifiedName" => $clientInformation['clientName'],
        "CompanyName" => "43t334t",
        "DisplayName" => $clientInformation['clientName'],
        "PrimaryPhone" => [
            "FreeFormNumber" => $clientInformation['clientPhoneNumber']
        ],
        "PrimaryEmailAddr" => [
            "Address" => $clientInformation['clientEmail']
        ]
    ]);
    $resultingObj = $dataService->Add($theResourceObj);
    $error = $dataService->getLastError();
    if ($error) {
        echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
        echo "The Helper message is: " . $error->getOAuthHelperError() . "\n";
        echo "The Response message is: " . $error->getResponseBody() . "\n";
    }
    else {
        echo "Created Id={$resultingObj->Id}. Reconstructed response body:\n\n";
        $xmlBody = XmlObjectSerializer::getPostXmlFromArbitraryEntity($resultingObj, $urlResource);
        echo $xmlBody . "\n";

        return $resultingObj->Id;
    }

I get this error

PHP Fatal error: Uncaught QuickBooksOnline\API\Exception\ServiceException: Http Status Code [400]: Refresh OAuth 2 Access token with Refresh Token failed. Body: [{"error":"invalid_grant"}].\n\n thrown in /var/www/project1/vendor/quickbooks/v3-php-sdk/src/Core/OAuth/OAuth2/OAuth2LoginHelper.php on line 271, referer: http://project1.local/trial/

What am I doing wrong. Thank you.

fabrik
  • 14,094
  • 8
  • 55
  • 71
  • Running into similar error, can you post how you used refresh access token to generate new access / refresh key? – AJK Jan 18 '19 at 22:13
  • @AJ47 did you manage to come right .. I have the same issue – the_big_blackbox Oct 26 '19 at 17:11
  • @the_big_blackbox not quite, I had posted a similar question but didn't get the answer I was looking for, https://stackoverflow.com/questions/54278156/ – AJK Oct 28 '19 at 15:13

2 Answers2

1
  • First of all you have generate refresh token and accesstoken manually first time.
  • Because first time to get refresh token and access token need auth code.
  • So you must generate authcode after that generate refresh token and accesstoken
  • After that you can try that it's working fine

Note: If you are get Invalid grant then you must generate authcode after that you have generate accesstoken and referesh token

0
use QuickBooksOnline\API\DataService\DataService;

$dataService = DataService::Configure(array(
    'auth_mode' => 'oauth2',
    'ClientID' => 'your client id',
    'ClientSecret' => 'your client secret',
    'RedirectURI' =>'redirect url',
     'scope' => "com.intuit.quickbooks.accounting openid profile",
     'baseUrl' => 'development or production'
));

$OAuth2LoginHelper = $dataService->getOAuth2LoginHelper();
$authorizationCodeUrl = $OAuth2LoginHelper->getAuthorizationCodeURL();

if( isset($_GET['code']) ) {
    $accessTokenObj = $OAuth2LoginHelper->exchangeAuthorizationCodeForToken( $_GET['code'], 'your company id') );

    // save these for later use

    $refreshTokenValue = $accessTokenObj->getRefreshToken();
    // Expires every 12 hours.
    $refreshTokenExpiry = $accessTokenObj->getRefreshTokenExpiresAt();

    // The access token and access token expiration.
    $accessTokenValue = $accessTokenObj->getAccessToken();
    $accessTokenExpiry = $accessTokenObj->getAccessTokenExpiresAt();
}