只能获取 QuickBooks API 的访问令牌一次
Can only get access token for QuickBooks API once
我正在使用 PHPoAuthLib in order to connect to the QuickBooks API per their example
当我以他们为榜样时,我向 API 发出的第一个请求完美运行:
$result = json_decode($quickbooksService->request($url));
echo 'result: <pre>' . print_r($result, true) . '</pre>';
但是在他们的示例中,他们使用 $_GET['oauth_token']
和 $_GET['oauth_verifier']
来请求访问令牌,并且这些值仅在来自 QuickBooks Online 的单个回调期间在 $_GET
服务器变量上可用在我的应用程序获得授权后立即。
对于以后的请求,PHPoAuthLib 的文档中没有此类示例,因此我尝试了一个快速的自制解决方案:
- 将 QBO 的回复保存在某处
if (!empty($_GET['oauth_token']) {
file_put_contents("token.txt", json_encode([
'oauth_token' => $_GET['oauth_token'],
'oauth_verifier' => $_GET['oauth_verifier'],
'realm_id' => $_GET['realmId']
]));
}
- 稍后再次使用该回复
$token = json_decode(file_get_contents("token.txt"));
$quickbooksService->requestAccessToken(
$token->oauth_token,
$token->oauth_verifier
// $token->getRequestTokenSecret() is not necessary - it will be automatically populated
);
// At this point my app crashes and return a 500 error
// Further code does not run
我收到的错误是:
TokenResponseException in StreamClient.php line 68:
Failed to request resource. HTTP Code: HTTP/1.1 401 Unauthorized
请记住,如果我在应用程序获得授权后立即使用令牌和验证器,它们会完美。如果我将它们保存到一个文件中并在 30 秒后尝试重新使用它们,就会发生这种情况。
我认为这可能是对 OAuth 1.0 的基本误解
我不认为您拥有的是正确的 OAuth 实现。您是否阅读过 OAuth 规范并按其定义实施?
获得请求令牌和验证器后,您可以使用它们来获取访问令牌。
该访问令牌有效期为 6 个月。
您似乎正在尝试使用短期请求令牌来持续获取访问令牌。那不行。
即如果您每次想发出另一个请求时都这样做:
$quickbooksService->requestAccessToken(
那你做错了。您应该每 6 个月执行一次,仅此而已。
这里的工作代码:
- https://github.com/consolibyte/quickbooks-php/blob/master/QuickBooks/IPP/IntuitAnywhere.php
- https://github.com/consolibyte/quickbooks-php/blob/master/QuickBooks/IPP/OAuth.php
- https://github.com/consolibyte/quickbooks-php
规格在这里:
我正在使用 PHPoAuthLib in order to connect to the QuickBooks API per their example
当我以他们为榜样时,我向 API 发出的第一个请求完美运行:
$result = json_decode($quickbooksService->request($url));
echo 'result: <pre>' . print_r($result, true) . '</pre>';
但是在他们的示例中,他们使用 $_GET['oauth_token']
和 $_GET['oauth_verifier']
来请求访问令牌,并且这些值仅在来自 QuickBooks Online 的单个回调期间在 $_GET
服务器变量上可用在我的应用程序获得授权后立即。
对于以后的请求,PHPoAuthLib 的文档中没有此类示例,因此我尝试了一个快速的自制解决方案:
- 将 QBO 的回复保存在某处
if (!empty($_GET['oauth_token']) {
file_put_contents("token.txt", json_encode([
'oauth_token' => $_GET['oauth_token'],
'oauth_verifier' => $_GET['oauth_verifier'],
'realm_id' => $_GET['realmId']
]));
}
- 稍后再次使用该回复
$token = json_decode(file_get_contents("token.txt"));
$quickbooksService->requestAccessToken(
$token->oauth_token,
$token->oauth_verifier
// $token->getRequestTokenSecret() is not necessary - it will be automatically populated
);
// At this point my app crashes and return a 500 error
// Further code does not run
我收到的错误是:
TokenResponseException in StreamClient.php line 68: Failed to request resource. HTTP Code: HTTP/1.1 401 Unauthorized
请记住,如果我在应用程序获得授权后立即使用令牌和验证器,它们会完美。如果我将它们保存到一个文件中并在 30 秒后尝试重新使用它们,就会发生这种情况。
我认为这可能是对 OAuth 1.0 的基本误解
我不认为您拥有的是正确的 OAuth 实现。您是否阅读过 OAuth 规范并按其定义实施?
获得请求令牌和验证器后,您可以使用它们来获取访问令牌。
该访问令牌有效期为 6 个月。
您似乎正在尝试使用短期请求令牌来持续获取访问令牌。那不行。
即如果您每次想发出另一个请求时都这样做:
$quickbooksService->requestAccessToken(
那你做错了。您应该每 6 个月执行一次,仅此而已。
这里的工作代码:
- https://github.com/consolibyte/quickbooks-php/blob/master/QuickBooks/IPP/IntuitAnywhere.php
- https://github.com/consolibyte/quickbooks-php/blob/master/QuickBooks/IPP/OAuth.php
- https://github.com/consolibyte/quickbooks-php
规格在这里: