0

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.

Any help would be much appreciated.

  • This is the response that comes up when clicking through the link.. How do I change the consumer key in the token I've already got? oauth_problem=consumer_key_unknown&oauth_problem_advice=Consumer%20key%20was%20not%20recognised – Charlie Sweetland Dec 01 '20 at 12:28
  • A `consumer_key...` response indicates that you haven't provided an Authorization header in the form of `Bearer {YOUR_TOKEN}` - requests without that format go through to the old OAuth1.0a gateway. Can you check your request as it goes out to make sure you have this header? – rustyskates Dec 01 '20 at 19:44

1 Answers1

0

I was actually searching too see if anyone else is trying to use R with Xero when this popped up.

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::GET("https://api.xero.com/api.xro/2.0/Invoices", config = token, httr::add_headers(`Xero-tenant-id` = tenant_id))
novica
  • 655
  • 4
  • 11