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.