duanshan3331
2017-04-12 06:35 阅读 44
已采纳

Quickbooks Online API中的ApplicationAuthenticationFailed错误

When I run the Quickbooks Online API using 'Postman', it works fine. Postman automatically generates the nonce and oauth_signature value. But when I run the API using PHP, i got stuck with the following error.

message=ApplicationAuthenticationFailed; errorCode=003200; statusCode=401 SignatureBaseString: POST&https%3A%2F%2Fsandbox-quickbooks.api.intuit.com%2Fv3%2Fcompany%2F193514525%2Faccount&oauth_consumer_key%3DqyprdH7kq5iG34sV2fSaj9tKC1KzXG%26oauth_nonce%3DFWXxbP%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1491976910%26oauth_token%3DqyprdDXeKDQonYAz8O0OSnyewIsg34HjxZyH0X4Dk5dNE5LC%26oauth_version%3D1.0

I think it may be related to generating signature value. I have used PHP '$oauth->generateSignature()' function to generate the signature. It generates the value. But the authorization error still occurs. Please help me to solve the issue.

Here I am providing the codes that I am used.

$ar =   array(
'authorization: OAuth oauth_consumer_key="'.OAUTH_CONSUMER_KEY.'",oauth_token="'.ACCESS_TOKEN_SECRET.'",oauth_signature_method="HMAC-SHA1",oauth_timestamp="'.$time.'",oauth_nonce="'.$randomvalue.'",oauth_version="1.0",oauth_signature="'.$signaturevalue.'"',
"cache-control: no-cache",
"content-type: application/json",
"Accept: application/json"

);

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://sandbox- quickbooks.api.intuit.com/v3/company/{{companyid}}/account",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{
  \"AccountType\": \"Accounts Receivable\",
  \"Name\": \"MyJobs\"
}",
CURLOPT_HTTPHEADER =>  $ar,
));
$response = curl_exec($curl);

This is the code I used for creating an account. I used following code to generate signature.

    $oauth = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET);
    $signature  =   $oauth->generateSignature('POST','https://sandbox-quickbooks.api.intuit.com/v3',$params);

In this case $params array defined as follows

     $params = array(
        'oauth_consumer_key' => OAUTH_CONSUMER_KEY,
        'oauth_nonce' => $randomvalue,
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_timestamp' => $time,
        'oauth_version' => '1.0',
        'oauth_token' => ACCESS_TOKEN
    );
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dtbi27903 dtbi27903 2017-04-12 14:31

    You should use a library that handles all of this stuff for you -- there are lots of issues in your code. Here's a library that handles all of this for you:

    Follow the quick-start on that page.

    Specific issues I can see with your code:

    You can't have a constant access token. It changes every 5 to 6 months, and thus is not a constant:

    oauth_token="'.ACCESS_TOKEN_SECRET.'"

    This URL has two issues:

    • It has a space in it
    • The company ID is an integer and here it looks like you put a placeholder instead

    CURLOPT_URL => "https://sandbox- quickbooks.api.intuit.com/v3/company/{{companyid}}/account",

    This URL is incorrect -- you're signing a different URL than you're POSTing to:

    $signature = $oauth->generateSignature('POST','https://sandbox-quickbooks.api.intuit.com/v3',$params);

    点赞 评论 复制链接分享

相关推荐