Quickbooks 创建客户 OAuth 2.0 令牌错误

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;
    }

我收到这个错误

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/

我做错了什么。谢谢。

  • 首先你已经手动生成刷新令牌和访问令牌 第一次。
  • 因为第一次拿到refresh token和access token 需要授权码。
  • 因此您必须在生成刷新令牌之后生成授权码 和访问令牌
  • 之后你可以尝试它工作正常

注意:如果您获得无效授权,那么您必须在生成访问令牌和引用令牌后生成授权码

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