簡體   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