gpt4 book ai didi

node.js - Xero SDK - 状态参数的OAuth实现

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:09 32 4
gpt4 key购买 nike

我目前已经使用 xero-node sdk 包实现了 NodeJs、express api,并且遇到了一个问题,似乎没有使用 OAuth 状态参数(尽管我看到它被定义为 XeroClient 构造函数的可选参数:

export interface IXeroClientConfig {
clientId: string,
clientSecret: string,
redirectUris: string[],
scopes: string[],
state?: string
}

谁能确认这是否已实现?

我假设它会像这样工作:

const xero = new XeroClient({
clientId: xeroParams.clientId,
clientSecret: xeroParams.clientSecret,
redirectUris: [xeroParams.redirectUrl],
scopes: xeroParams.scopes.split(' '),
state: this.callback_state,
});

//then when building the consent url like this, the state param would be included
const consentUrl = await xero.buildConsentUrl();

然后,当回调被触发时,我希望能够访问状态代码作为查询参数之一。类似的事情解释了here

我已经看到返回了 session_state 参数,但这与我提供的状态代码不匹配。

最佳答案

以下是如何使用 xero-node SDK 通过 OAuth 流程传递状态:

https://github.com/SerKnight/xero-node-basic-app/blob/master/index.js#L37

示例:

  • 首先生成consentUrl,然后附加您的自定义参数。
app.get('/connect', async function(req, res) {
try {
let consentUrl = await xero.buildConsentUrl();
res.redirect(consentUrl + "&state=THIS_IS_A_STANDARD_OAUTH_2_STATE_PARAMETER"); // Append any type of state/params you would like
} catch (err) {
res.send("Sorry, something went wrong");
}
})

...

app.get('/callback', async function(req, res) {
let url = redirectUri + req.originalUrl;

console.log('req.query: ', req.query) // this will the the same state/params you passed to the API

// ...do api stuff..
// ref: https://github.com/XeroAPI/xero-node-oauth2-app

res.send(req.query);
})

https://github.com/SerKnight/xero-node-basic-app/blob/master/index.js#L37

值得注意的是,XeroClient 的可选 state: 参数是为 openidclient 的东西保留的。不要使用它。只需将其附加到同意网址即可。

关于node.js - Xero SDK - 状态参数的OAuth实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59994037/

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