首页 > 解决方案 > QuickBooks API 示例显示了“curl”的用法,但它实际上不起作用或者我错过了什么

问题描述

根据QuickBooks Ruby API 文档,它提供了一个使用curl向从步骤 1 获得的授权 url 提交 POST 请求的示例,但在其下方,它显示如果 Web 应用程序不支持浏览器,则使用 Playground 或网页组件。

我的问题是——真的可以使用示例中所示的 curl 命令吗?如果我采用确切的 URL 并尝试建立 POST 请求,我会得到以下结果:

bash-3.2$ curl -X POST "https://appcenter.intuit.com/connect/oauth2?client_id=[redacted]&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=com.intuit.quickbooks.accounting&state=be17472c59724eb46bfe2690"
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
bash-3.2$

显然它正在尝试重定向到另一个 URL,但 API 文档没有显示使用该-L参数来跟踪重定向。如果我尝试为跟随重定向提供-L参数,那么我会得到以下响应:curl

bash-3.2$ curl -X POST "https://appcenter.intuit.com/connect/oauth2?client_id=[redacted]&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=com.intuit.quickbooks.accounting&state=be17472c59724eb46bfe2690" -L
<HTML><HEAD>
<TITLE>Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<P>
Reference&#32;&#35;7&#46;2500e8ac&#46;1592267832&#46;14229c52
</BODY>
</HTML>

同样,即使遵循重定向也不起作用。如果我附加-Icurl命令,那么我得到以下内容:

bash-3.2$ curl -X POST "https://appcenter.intuit.com/connect/oauth2?client_id=[redacted]&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=com.intuit.quickbooks.accounting&state=be17472c59724eb46bfe2690" -LI
HTTP/2 301
date: Tue, 16 Jun 2020 00:37:17 GMT
content-type: text/html
content-length: 162
location: https://appcenter.intuit.com/app/connect/oauth2?client_id=[redacted]&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=com.intuit.quickbooks.accounting&state=be17472c59724eb46bfe2690
server: nginx
cache-control: no-store, no-cache, must-revalidate
strict-transport-security: max-age=3156000; includeSubDomains; preload

HTTP/2 302
date: Tue, 16 Jun 2020 00:37:17 GMT
content-type: text/plain;charset=utf-8
content-length: 406
location: https://accounts.intuit.com/index.html?partner_uid_button=google&offering_id=Intuit.sbg-fms.ippdevx&redirect_url=https%3A%2F%2Fappcenter.intuit.com%2Fapp%2Fconnect%2Foauth2%3Fclient_id%3D[redacted]%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%26response_type%3Dcode%26scope%3Dcom.intuit.quickbooks.accounting%26state%3Dbe17472c59724eb46bfe2690
server: nginx
strict-transport-security: max-age=15552000
intuit_tid: 1-5ee8143d-29a68cec2ec922da5c6be528
x-spanid: ad76586b-a5ac-41bd-b2df-022148a5a78b
x-amzn-trace-id: Self=1-5ee8143d-3aa839e8b2cf25d846078238;Root=1-5ee8143d-29a68cec2ec922da5c6be528
x-dns-prefetch-control: off
x-download-options: noopen
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
cache-control: private,no-cache,no-store,pre-check=0,post-check=0,must-revalidate
expires: -1
pragma: no-cache
set-cookie: hosted-shell=%7B%22clientId%22%3A%22a4676677-5191-4eca-873e-34a04b5b1dd4%22%7D; Path=/; Expires=Fri, 14 Jun 2030 00:37:17 GMT; Secure
vary: Accept, Accept-Encoding
cache-control: no-store, no-cache, must-revalidate
strict-transport-security: max-age=3156000; includeSubDomains; preload

HTTP/2 411
server: AkamaiGHost
mime-version: 1.0
content-type: text/html
content-length: 223
expires: Tue, 16 Jun 2020 00:37:17 GMT
date: Tue, 16 Jun 2020 00:37:17 GMT

如果我采用相同的 URL 并通过 Web 浏览器请求它,那么它只会将我带到应有的重定向回调 URL。我不明白为什么curl不做同样的事情。如果我什至不能通过 curl 复制相同的东西,我不明白“Web 组件”是什么意思。我还有另一个用于 GET 和 POST 请求的 Web 模块,它们将我带到 HTTP200 但不是回调 URL。

知道如何像在 Web 浏览器中那样从重定向回调 URL 获取此授权令牌吗?这是我尝试通过 Ruby 脚本中的 Web 组件执行此操作的另一个示例:

[3] pry(#<QuickBooksAPI>)> grant_url
=> "https://appcenter.intuit.com/connect/oauth2?client_id=[redacted]&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=com.intuit.quickbooks.accounting&state=be17472c59724eb46bfe2690"
[4] pry(#<QuickBooksAPI>)> response = WebRequest.new.get_request(grant_url)
=> #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true>
[5] pry(#<QuickBooksAPI>)> response['Location']
=> "https://appcenter.intuit.com/app/connect/oauth2?client_id=[redacted]&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=com.intuit.quickbooks.accounting&state=be17472c59724eb46bfe2690"
[6] pry(#<QuickBooksAPI>)> response = WebRequest.new.get_request(response['Location'])
=> #<Net::HTTPFound 302 Found readbody=true>
[7] pry(#<QuickBooksAPI>)> response['Location']
=> "https://accounts.intuit.com/index.html?partner_uid_button=google&offering_id=Intuit.sbg-fms.ippdevx&redirect_url=https%3A%2F%2Fappcenter.intuit.com%2Fapp%2Fconnect%2Foauth2%3Fclient_id%3D[redacted]%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%26response_type%3Dcode%26scope%3Dcom.intuit.quickbooks.accounting%26state%3Dbe17472c59724eb46bfe2690"
[8] pry(#<QuickBooksAPI>)> response = WebRequest.new.get_request(response['Location'])
=> #<Net::HTTPOK 200 OK readbody=true>
[9] pry(#<QuickBooksAPI>)> response.uri
=> #<URI::HTTPS https://accounts.intuit.com/index.html?partner_uid_button=google&offering_id=Intuit.sbg-fms.ippdevx&redirect_url=https%3A%2F%2Fappcenter.intuit.com%2Fapp%2Fconnect%2Foauth2%3Fclient_id%3D[redacted]%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%26response_type%3Dcode%26scope%3Dcom.intuit.quickbooks.accounting%26state%3Dbe17472c59724eb46bfe2690>

这一次它将我发送到 200,但它实际上从未将我发送到回调/重定向 URL。

我只是简单地尝试完成第 2 步,但似乎没有浏览器就无法完成,即使使用“Web 组件”和 curl。

我是否只需要手动使用 OAuth Playground 并不断刷新我的令牌,因为我的 API 都是后端并且没有前端/用户重定向等?

标签: rubycurlredirectoauth-2.0quickbooks

解决方案


OAuth 授权流程需要您的最终用户在您使用访问令牌调用 API 之前授予您对应用程序的身份验证。所以这就是你处理的样子

  1. 向您的用户展示连接到 Intuit(或等效)按钮
  2. 用户单击按钮并在 intuit 网站上进行身份验证(似乎是您在问题中插入的第一段代码。这不能自动化)
  3. Intuit 使用授权码重定向到重定向 url
  4. 休息是自动化的 - 您的浏览器或服务器从 url 获取授权码,将其与一些其他参数结合起来并请求 access_token
  5. 然后,您可以继续使用 access_token 代表最终用户拨打电话。

从第 3 步开始。之后,您可以使用 curl 来处理所有这些。希望这可以帮助 :)

编辑

不,在您的情况下,您会以不同的方式使用 OAuth。使用 Oauth Playground 将为您创建一个初始 access_token。步骤 1-4 已处理完毕。

现在您可以使用此令牌来验证您的所有 API 调用。但是,令牌将过期。因此,当您确实收到过期响应时,您必须使用此处的信息进行 curl 以获取新令牌...

https://developer.intuit.com/app/developer/qbo/docs/develop/authentication-and-authorization/oauth-2.0#refresh-the-token


推荐阅读