QuickBooks API示例显示了“ curl”的用法,但实际上不起作用,或者我缺少某些东西

问题描述 投票:0回答:1

根据QuickBooks Ruby API documentation,它提供了一个示例,该示例使用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>

同样,即使重定向后也不起作用。如果将-I附加到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" -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。

您知道如何像在网络浏览器中那样从重定向回调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都是后端,并且没有前端/用户重定向,等等?

ruby curl redirect oauth-2.0 quickbooks
1个回答
0
投票

OAuth授予流程需要您的最终用户授予您对应用程序进行身份验证的权限,然后才能使用访问令牌调用API。所以这就是您要处理的样子

  1. 通过“连接到Intuit(或等效按钮)向您的用户显示”
  2. 用户单击按钮并通过intuit网站进行身份验证(好像您在问题中插入的第一部分代码。这不能自动进行)
  3. Intuit使用授权码重定向到重定向URL
  4. Rest是自动的-您的浏览器或服务器从网址中获取authorization_code,并将其与其他一些参数结合,并请求一个access_token
  5. 然后您可以继续使用access_token代表最终用户拨打电话。

从Step3。以后,您可以使用curl处理所有内容。希望这会有所帮助:)

编辑

不,根据您的情况,您将以其他方式使用OAuth。使用Oauth Playground将为您创建一个初始access_token。步骤1-4已完成。

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

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

我相信Pathfix在这里很方便。他们执行步骤1-4,然后您可以将您的API请求传递到他们的服务器,他们将管理令牌的到期日及所有。争取他们的支持,他们会非常有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.