API OAuth 2.0 - XERO 以 R 接收状态 400 访问

API OAuth 2.0 - XERO acces with R receiving status 400

我们必须迁移到 Oauth 2.0。但是,我面临以下问题。状态 400 我相信是由于某些参数可能没有正确输入。如果有人知道哪里出了问题或有任何想法,请告诉我。

这是我的代码:

pack <- c('curl','xml2','XML', 'plyr', 'dplyr','tidyr', 'httr', 'tools', 'lubridate',
          'jsonlite', 'stringr', 'data.table', 'anytime')
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 <- 'YOUR_REFERAL_ID'#Referral_ID 
Redirect_URI <- 'https://xero.com/' #OAuth 2.0 redirect URI

response <- GET(paste0('https://login.xero.com/identity/connect/authorize?response_type=code&client_id=',
       Client_ID,'&redirect_uri=',Redirect_URI,'&scope=workflowmax%20offline_access'))
browseURL(response$url)

它一直有效,直到我能够检索到第一个代码,但我无法获得令牌 这是 Xero Doc 所说的:

Xero 文档请求:

POST https://identity.xero.com/connect/token
authorization: "Basic " + base64encode(client_id + ":" + client_secret)
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=xxxxxx
&redirect_uri=https://myapp.com/redirect

现在尝试获取令牌我尝试了几种形式。但是,它们看起来真的很相似。

code <- 'YOUR_CODE'

credentials = list();
credentials['grant_type'] <- "authorization_code"
credentials['code'] <- code
credentials['redirect_uri'] <- Redirect_URI

b64_id_secret <- base64_enc('YOUR_CLIENT_ID:YOUR_CLIENT_SECRET')
# user <- paste("Basic", user, sep = " ")
url <- 'https://identity.xero.com/connect/token'    
POST(url, add_headers('Authorization'= paste0('Basic ', b64_id_secret)), 
         body  = credentials,
         verbose(), encode = 'form')

回复:

Response [https://identity.xero.com/connect/token?grant_type=authorization_code&code=YOUR_CODE&redirect_uri=https://waterlineprojects.com/]
  Date: 2020-11-20 02:48
  Status: 400
  Content-Type: application/json; charset=UTF-8
  Size: 27 B

XERO 文档:https://developer.xero.com/documentation/oauth2/overview

看起来你很接近。就像 MrFlick 所说的那样,如果没有您的客户端 ID 并开始使用 Xero API 技术支持(电子邮件 api@xero.com 以及您的客户端 ID 和日志数据),很难确定。

可能是一个提示,您的重定向 URI 必须 完全匹配 https://developer.xero.com/myapps/details?appId= 包含结束斜杠的仪表板。

此外 - 400 错误的正文是什么,应该是这样的

{
  "error": "invalid_grant"
}

这将帮助您更容易地推断出问题所在。

https://developer.xero.com/documentation/oauth2/troubleshooting

我做了我的解决方法并得到了它 运行。这是访问Oauth 2.0的方法。

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_ID'
Client_secret<- 'YOUR_SECRET'

XTID_Xero <- 'YOUR_XTID'#Referral_ID 
Redirect_URI <- 'YOUR_CALL_BACK_URL' #OAuth 2.0 redirect URI

# Create the app
app <- oauth_app("YOUR_API_NAME",
                 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
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 <- 'YOR_CODE'
state_xero <- 'YOUR_STATE'

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)