简体   繁体   English

使用 r 中的 Oauth2 在 403 代码后授权访问 Xero

[英]Authorising access to Xero after 403 code using the Oauth2 in r

This is the step after the question that can be found here这是可以在此处找到的问题之后的步骤

I've run the following code:我已经运行了以下代码:

pack <- c('curl','xml2','XML', 'plyr', 'dplyr','tidyr', 'httr', 'tools', 'lubridate',
      'jsonlite', 'stringr', 'data.table', 'anytime', 'RCurl', 'rvest', 'opnessl', 'jose')

sapply(pack, function(x){ 
  if(!require(x,character.only = T, quietly = T)) {install.packages(x, quiet = T)}
  require(x, quietly = T, character.only = T)
})

#New Xero & WFM Api OAuth 2.0 credentials
Client_ID <- 'Your_Client_ID '
Client_secret<- 'Your_Client_secret'

XTID_Xero <- 'x30rscript'#Referral_ID 
Redirect_URI <- 'https://developer.xero.com/' #OAuth 2.0 redirect URI

# Create the app
app <- oauth_app("RScript",
                 key = Client_ID,
                 secret = Client_secret,
                 redirect_uri = Redirect_URI
                 
)
# Create the endpoint
create_endpoint <- function()
{
  request <- "https://identity.xero.com/connect/token"
  authorize <- "https://login.xero.com/identity/connect/authorize"
  access <- "https://identity.xero.com/connect/token"
  httr::oauth_endpoint(request, authorize, access)
}
api <- create_endpoint()

header <- httr::add_headers(Authorization=paste0("Basic ", RCurl::base64Encode(charToRaw(paste0(Client_ID, ":", Client_secret)))))
content_type <- httr::content_type("application/x-www-form-urlencoded")

# Define the scope - https://developer.xero.com/documentation/oauth2/scopes
scope_WFM <- "openid profile offline_access payroll.employees.read payroll.payruns.read payroll.payslip.read payroll.timesheets.read accounting.transactions.read accounting.reports.read accounting.journals.read"

# Get the code 
httr::BROWSE(oauth2.0_authorize_url(api, app, scope = scope_WFM))

#get the code from the URL displayed in your browser
code_xero <- 'code_xero'
state_xero <- 'state_xero'

token <- httr::oauth2.0_token(
  endpoint = api,
  app = app,
  scope = scope_WFM,
  config_init = c(header, content_type),
  use_basic_auth = TRUE,
  query_authorize_extra = list(prompt = "login"),
  type = "code",
  credentials = oauth2.0_access_token(api, app, code_xero),
  cache = FALSE
)

#get your xero-tenant-id
access <- GET("https://api.xero.com/connections", config = token)
connections <- content(access, 'text')
connections <- fromJSON(connections, flatten = T)
connections

# This is where I get an error.... 
GET("https://api.xero.com/api.xro/2.0/banktransactions", 
    config = token,
    authenticate(Client_ID, Client_secret))

This returns the following:这将返回以下内容:

Response [https://api.xero.com/api.xro/2.0/banktransactions]
  Date: 2020-12-01 10:59
  Status: 403
  Content-Type: application/json
  Size: 150 B

I'm trying to access Xero to pull out the current cash position, Debtors Days, Turnover/Profit/Growth and I'm struggling to a response from any of the areas I need in R.我正在尝试访问 Xero 以提取当前的现金 position、债务人天数、营业额/利润/增长,我正在努力对 R 中我需要的任何领域的回应。

Any help would be much appreciated.任何帮助将非常感激。

I was actually searching too see if anyone else is trying to use R with Xero when this popped up.我实际上也在搜索,看看有没有其他人在弹出这个问题时试图将 R 与 Xero 一起使用。

So in case anyone else needs this, the solution is to pass the token with the headers.因此,如果其他人需要这个,解决方案是将令牌与标头一起传递。 Using httr::add_headers this would be like so:使用httr::add_headers这将是这样的:

httr::GET("https://api.xero.com/api.xro/2.0/Invoices", config = token, httr::add_headers(`Xero-tenant-id` = tenant_id))

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

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