Спасибо за комментарии. Теперь у меня есть эта работа, поэтому я отправляю свой ответ всем, у кого есть такие же проблемы.
После привязки вашего приложения, использующего oauth2, в Xero Developer, первым шагом в oauth2 будет шаг «трех ног», где у вас должно быть одобрение пользователя. Мне не удалось заставить работать Chilkat, пример (перенаправление запрещено). Однако, поскольку обычно вам нужно сделать это только один раз, достаточно просто использовать xoauth Terminal script, который Xero предоставляет . По сути, вы просто:
- запускаете настройку xoauth, обязательно добавляя области offline_access (так вы получите жизненно важный токен refre sh) и любые области доступа к данным организации, которые вам нужны, например учет. Транзакции и учет .contacts
- запустить xoauth connect
- скопировать и сохранить полученные access_token и refresh_token
Как только вы это сделаете, вы можете использовать access_token в своем приложении, обновляя его по мере необходимости с помощью refresh_token (который меняется каждый раз, когда вы это делаете).
Вот как это сделать, используя плагины Chilkat:
// Set up the Authorisation:
Dim oauth2 As New Chilkat.OAuth2
'Set up from saved data:
oauth2.ClientId = { Client id used in initial setup }
oauth2.AccessToken = { access_token retrieved in setup }
oauth2.RefreshToken = { refresh_token: first retrieved in setup, then from each refresh }
oauth2.TokenEndpoint = "https://identity.xero.com/connect/token"
yourTenantID = { the <OrganisationID> of the <Organisation> you will be connecting to }
Dim success As Boolean
Dim rest As Chilkat.Rest
rest.Authorization = "Bearer " + oauth2.AccessToken
success = rest.AddHeader( "xero-tenant-id", yourTenantID)
'Responses will be in json: if you want xml, add this:
success = rest.AddHeader( "accept", "application/xml" )
Остальной объект теперь готов к использовать для взаимодействия с Xero. Например, чтобы получить данные:
// Get some data:
'endpoint is the endpoint you want, e.g. "Accounts"
'identifier and condition are optional parameters, see Xero's API Previewer
Dim sbPath As New Chilkat.StringBuilder
success = sbPath.Append( "/api.xro/2.0/" + endpoint )
if success then
if identifier>"" then
success = sbPath.Append("/" + identifier)
elseif condition>"" then
success = rest.AddQueryParam( "where" ,condition )
end if
end if
if not success then return rest.LastErrorText
// Get the full or matching list:
Dim sbXml As New Chilkat.StringBuilder
success = rest.FullRequestNoBodySb( "GET", sbPath.GetAsString(), sbXml)
dim statuscode As Integer = rest.ResponseStatusCode
if statuscode = 401 then
'access code expired: use refresh token to revive it and get new refresh token
success = oauth2.RefreshAccessToken()
if success then
{ Save the new oauth2.RefreshToken }
{ Disconnect and reconnect rest }
success = rest.FullRequestNoBodySb( "GET", sbPath.GetAsString(), sbXml)
end if
end if
if success then
If (rest.ResponseStatusCode <> 200) Then // A 200 response is expected for actual success
return sbXml.GetAsString()
else // get the response, in this case XML
Dim bAutoTrim As Boolean = True
success = xml.LoadSb(sbXml,bAutoTrim)
end if
end if
if not success then return rest.LastErrorText