快速书籍创建客户OAuth 2.0令牌错误

时间:2018-09-24 11:51:41

标签: php api quickbooks

    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致命错误:未捕获的QuickBooksOnline \ API \ Exception \ ServiceException:Http状态代码[400]:刷新带有刷新令牌的OAuth 2访问令牌失败。正文:[{“ error”:“ invalid_grant”}]。\ n \ n在/var/www/project1/vendor/quickbooks/v3-php-sdk/src/Core/OAuth/OAuth2/OAuth2LoginHelper.php中抛出271,引荐来源:http://project1.local/trial/

我在做什么错。谢谢。

2 个答案:

答案 0 :(得分:1)

  • 首先,您已手动生成刷新令牌和accesstoken 第一次。
  • 因为是第一次获取刷新令牌和访问令牌 需要验证码。
  • 因此,您必须在生成刷新令牌之后生成身份验证码 和访问令牌
  • 之后,您可以尝试它的正常运行

注意:如果获得无效授权,则必须在生成accesstoken和referesh令牌之后生成authcode

答案 1 :(得分: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();
}