无法生成playwright-javascript API端点basic-auth访问令牌(令牌生成)

iyzzxitl  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(168)

POSTMAN请求:

TokenEndPoint - https://api.dev.aa.com//token
Authorization: username: 'fadsfadf353423',
               password: 'ZurbhG3kjZ'
Body: grant_type: 'client_credentials'

curl :

curl --location --request POST 'https://api.dev.aa.com/edgemicro-auth/token' \
--header 'Authorization: Basic UkdZZUxSVUpobk5iRkR1QXJ4dzBnV1JNYXY3QmRxS==' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: _abck=D615B497E043C6F7E72E5F9AB4A34B5E~-1~YAAQDPs7FyzSrg+HAQAAwuC2NwmgIuawZv+zP3GhCafLPgqXpwKMPc9pjZ1IQ1kkIaWLFigGwzSfbIV6Y0cA/octAN264XvJR7IQHlVWNKSW64iR3bKb/4cTCCoKtNVyX3V7O+ekwHo/MhiK2Ie3vzGIw5qXpeqX7VQLSshZ4Tn+YvWCoARgFpSh0H6WhJQqPoKlvl3JJkMogFubx6csEfoHvShZaOdi2E0ddNxQOV2bmSnBtHuN5ntsIskySSqI1NvdXquXIZxyq+ghkDxgCWHaW9uFdLsZChBHZb7MhwSEfXvAeuBC4gSEhg5T8dHltZpYR+DvJQlZPdGh2fccOE8jb07ypbEU+FK5djoEk0qZJypQ6mJyGKqwYnBw6XvdRbBl~-1~-1~1675889701' \
--data-urlencode 'grant_type=client_credentials'

编剧:我试过了:

import { expect, request, test } from "@playwright/test";
    const apiUrlEndPoint = 'https://api.dev.aa.com/token'
     test('authendPoin', async ({request}) => {
        const response = await request.post(apiUrlEndPoint, {
            headers: {
                //grant_type: 'client_credentials',
                grant_type: 'password',
                username: 'RGYeLRUJhnNbFDuArxw0gWRMav7BdqJu',
                password: 'ZurbhG3kjZviG4xV'
              }
            //data:processRefundBodyPayload
            //data: JSON.stringify(postBody),
        })
        expect(response.ok()).toBeTruthy
        console.log("===================Status Text==============")
        console.log(response.statusText())
        expect(response.status()).toBe(200)
  })

我得到的响应是401而不是200;但是,在POSTMAN中,它按预期状态代码200工作

eimct9ow

eimct9ow1#

解决方案如下:Playwright配置文件:

use: {
 baseURL: 'https://api.dev.aa.com',
 extraHTTPHeaders: {
 'content-Type': 'application/x-www-form-urlencoded',
 'Authorization': `Basic ${postman_curl_auth}`,
 },
}

剧作家规格文件

import { test as setup } from '@playwright/test';
setup('authenticate', async ({ request, baseURL }) => {
  const response = await request.post(`${baseURL}/auth/token`, {
    form: {
      username: "RGYeLRUJh35346474DFGDGEDDG",
      password: "ZurbhDHGDFTR4545G4xV",
      grant_type: 'password'
    },
  });
  console.log(response.statusText())
  const content = await response.json();
  console.log(content)
  const {access_token, id_token} = content;
});

相关问题