Search code examples
phpapiquickbooks

Quick Books Create Customer OAuth 2.0 token error


    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.


Solution

  • 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();
    }