如何使用Artillery自动生成OAuth 2.0令牌?

我想通过javascript自动执行OAuth 2.0令牌。我有什么办法可以做到这一点,并获得在火炮上使用它的令牌。

对于OAuth令牌生成,我有以下详细信息:

  • 验证网址
  • 客户ID
  • 范围

这是通过客户端身份验证凭据完成的。

以下是我用来生成令牌的示例代码:

var ClientOAuth2 = require('client-oauth2')

var Auth = new ClientOAuth2({
  clientId: 'ClientID',accessTokenUri: 'https://Auth_URL/v2.0/token',authorizationUri: 'https://Auth_URL/v2.0/authorize',redirecturi: 'https://Auth_URL/',scope: 'api://Scope/access_as_user'
})




  Auth.owner.getToken('username','password')
  .then(async (user) => {
    await console.log(user) //=> { accessToken: '...',tokenType: 'bearer',... }
  }).catch((e) => { console.log('error show',e); })
  .finally( () => console.log('end'));
wang312627 回答:如何使用Artillery自动生成OAuth 2.0令牌?

您可以声明您的自定义JS文件,该文件将在每次请求之前触发:

您的YAML文件可以如下所示:

config:
  target: "https://baseUrl.com"
  phases:
    - duration: 60
      arrivalRate: 100
  processor: "./customFile.js"

scenarios:
  - flow:
      - post:
          url: "/pathInYourApi"
          headers:
            Content-Type: "application/json"
            Accept: application/json
          json: {}
          beforeRequest: "beforeRequest"

,然后是您的customFile.js脚本:

module.exports = {
  beforeRequest: beforeRequest,};

function beforeRequest(requestParams,context,ee,next) {
  // Call your OAuth client,and after you obtain token you can assign it to requestParams Authorization header
  // eg. requestParams.headers.Authorization = `Bearer + ${token}`

  return next(); // MUST be called for the scenario to continue
}
本文链接:https://www.f2er.com/3159344.html

大家都在问