繁体   English   中英

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

[英]QuickBooks API example shows usage of “curl” but it actually doesn't work or am I missing something

According to QuickBooks Ruby API documentation , it provides an example of using curl to submit a POST request to the authorization url obtained from Step 1, but right underneath it, it shows that if the web application doesn't support browsers, to use Playground or web 组件。

我的问题是——实际上是否可以使用示例中所示的 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参数来跟踪重定向。 如果我尝试为curl提供-L参数以跟踪重定向,则会收到以下响应:

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>

同样,即使遵循重定向也不起作用。 如果我 append -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 组件”是什么意思。 我还有另一个 web 模块用于 GET 和 POST 请求,它们将我带到 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 都是后端并且没有前端/用户重定向等?

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

  1. 向您的用户展示连接到 Intuit(或等效)按钮
  2. 用户单击按钮并在 intuit 网站上进行身份验证(似乎是您在问题中插入的第一段代码。这不能自动化)
  3. Intuit 使用授权码重定向到重定向 url
  4. Rest 是自动化的 - 您的浏览器或服务器从 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM