gpt4 book ai didi

oauth-2.0 - 使用 Xojo 和 Chilkat 与 Xero 集成 OAuth2

转载 作者:行者123 更新时间:2023-12-04 09:14:01 26 4
gpt4 key购买 nike

旧的 Chilkat 论坛似乎已经搬到了这里。
多年来,我一直让 Chilkat 的 OAuth1 与 Xero(来自 Xojo)集成得很好。现在 Xero 正在用 OAuth2 替换 OAuth1。
Xero 表示,一旦您成功连接了 3-legged OAuth2,您就可以使用刷新 token 继续重新连接,而无需每次都经过 3-legged 过程(即用户同意允许您的连接)。因此,一旦您完成了 3-legged 一次,您就不需要再做一次:每个未使用的 Refresh token 的有效期为 60 天(但只能使用一次)。
所以这个过程似乎是:

  • 3-Leged 连接,手动批准,检索访问和刷新 token
  • 使用刷新 token ,获取所需数据和新的刷新 token ,无需手动批准。
  • 永远重复(2),如果出现问题,只需要再次执行(1)。

  • Chilkat OAuth2 Xojo 插件帮助显示了如何做 (1)。
    但是我不知道该怎么做(2)。我在使用 REST 方面远非专家(如果我是,我不会使用 Chilkat ;-))所以首选 Xojo 代码,但即使是伪代码也会有所帮助!
    谢谢

    最佳答案

    感谢您的评论。我现在已经完成了这项工作,所以我将我的答案发布给任何有同样问题的人。
    在 Xero Developer 中链接您使用 oauth2 的应用程序后,oauth2 的第一步是“3-legged”步骤,您必须获得用户的批准。我一直无法获得 Chilkat example工作(不允许重定向)。但是,由于您通常只需要执行一次,因此只需使用 xoauth Terminal script Xero provide 就很容易了。 .基本上你只是:

  • 运行 xoauth 设置,确保添加范围 offline_access(这样你
    获取重要的刷新 token )和任何组织数据访问
    您需要的范围,例如会计.交易和
    会计.联系人
  • 运行 xoauth 连接
  • 复制并保存生成的 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

    关于oauth-2.0 - 使用 Xojo 和 Chilkat 与 Xero 集成 OAuth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63297483/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com