【问题标题】:Quickbooks Online API Oauth2.0 using httrQuickbooks Online API Oauth2.0 使用 httr
【发布时间】:2018-03-09 05:20:01
【问题描述】:

我正在尝试通过 R 访问 QuickBooks 在线 API。这是我目前所拥有的:

library(httr)
library(httpuv)

endPoint <- oauth_endpoint(request = NULL,
                           authorize = "https://appcenter.intuit.com/connect/oauth2",
                           access = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer")
App <- oauth_app("Untitled",
                 key = "xxxx",
                 secret = "xxxxx",
                 redirect_uri = "http://localhost:1410/")

QBOtoken <- oauth2.0_token(endpoint = endPoint,
               app = App,
               scope = "com.intuit.quickbooks.accounting",
               type = "code",
               cache = T)
GET("https://sandbox-quickbooks.api.intuit.com/v3/company/193514718345164/query?query=Select * from Payment", config(token = QBOtoken))

当通过QBOtoken运行上面的代码时,我通过了整个Oauth2.0“跳舞”并得到了响应:

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.

但是,当我执行 GET 命令时,它会返回:

Error in self$credentials$access_token : 
  $ operator is invalid for atomic vectors

生成的.httr-oauth 文件填充了 284 行,每行 32 个字符。这正常吗?

我已经通过 Postman 连接到 API,它会生成一个访问令牌,允许我在我的代码中执行查询,例如 GET 请求。 Intuit Developer 上还有一个 Oauth 2.0 操场。不知何故,在 R 中,我没有获得访问令牌和刷新令牌。

另一个问题是我目前正在尝试连接到开发环境。对于 QuickBooks Online,重定向 URL 可以是开发环境中的 localhost,但如果我想连接到生产公司(我的公司)数据,我将需要一个 https 重定向 URL。这可以通过 R 实现吗?

我对这个项目的最终目标是让脚本每晚自动运行,连接到 API,并将数据 ETL 到关系数据库中以用于报告/分析目的。任何帮助将不胜感激!

【问题讨论】:

  • 嘿!你有没有想过这个???做同样的事情。
  • 嗨@Super_John,请在下面查看我的回答。这不是很好,自 3 月以来我还没有搞砸过,但它应该能让你朝着正确的方向前进!

标签: r oauth-2.0 postman quickbooks-online httr


【解决方案1】:

我确实得到了这个“功能性”,但它绝不是好的代码。其中一些,比如将令牌存储在 csv 中,并不是最佳实践,只是测试代码的创可贴。

我在结构中创建了一个 tokens.csv 文件:

    "RefreshToken","AccessToken"
    "<RefreshToken>","<AccessToken>"

下面是我从 QBO api 沙箱中检索客户数据的脚本。

library(httr)
library(httpuv)
library(curl)
library(jsonlite)
library(base64enc)

#Client ID and Client Secret were retrieved from the online explorer
clientID <- "<ClientID>"
clientSecret <- "<ClientSecret>"
scope <- "com.intuit.quickbooks.accounting"

tokens <- read.csv("tokens.csv")
RefreshToken <- as.character(tokens$RefreshToken[1])
AccessToken <- as.character(tokens$AccessToken[1])

authorize <- base64enc::base64encode(charToRaw(paste0(clientID,":",clientSecret)))

oauth_refresh <- httr::POST("https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer",
                            add_headers('Content-Type'= "application/x-www-form-urlencoded",
                                        'Accept'= 'application/json',
                                        'Authorization'= paste0('Basic ',authorize)
                                        ),
                            body = list('grant_type'='refresh_token',
                                        'refresh_token'=RefreshToken),
                            encode = "form")


oaJSON <- fromJSON(content(oauth_refresh, as = "text"))

RefreshToken <- oaJSON[["refresh_token"]][1]
AccessToken <- oaJSON[["access_token"]][1]

tokens <- as.data.frame(list('RefreshToken'=RefreshToken,'AccessToken'=AccessToken))
write.csv(tokens,file = "tokens.csv", row.names = F)

datas <- httr::GET("https://sandbox-quickbooks.api.intuit.com/v3/company/<ID>/query?query=SELECT%20%2a%20FROM%20Customer",
          accept_json(),
    add_headers('Authorization'= paste0("Bearer ",AccessToken))

)

#datas$status_code

j_son <- content(datas, as = "text")
customers <- fromJSON(j_son)
customer_df <- customers$QueryResponse$Customer

希望这能让你的球正确滚动。如果您有任何反馈,请告诉我!

【讨论】:

  • 很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多