1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-04 11:10:05 +00:00

Merge pull request #661 from sasjs/issue-660

fix(viya): updated getAccessTokenForViya with headers based on latest docs
This commit is contained in:
Allan Bowe
2022-03-01 17:53:04 +02:00
committed by GitHub
2 changed files with 11 additions and 20 deletions

View File

@@ -1,6 +1,5 @@
import { SasAuthResponse } from '@sasjs/utils/types'
import { prefixMessage } from '@sasjs/utils/error'
import * as NodeFormData from 'form-data'
import { RequestClient } from '../request/RequestClient'
/**
@@ -24,26 +23,17 @@ export async function getAccessTokenForViya(
token = Buffer.from(clientId + ':' + clientSecret).toString('base64')
}
const headers = {
Authorization: 'Basic ' + token
Authorization: 'Basic ' + token,
Accept: 'application/json'
}
let formData
if (typeof FormData === 'undefined') {
formData = new NodeFormData()
} else {
formData = new FormData()
}
formData.append('grant_type', 'authorization_code')
formData.append('code', authCode)
const data = new URLSearchParams({
grant_type: 'authorization_code',
code: authCode
})
const authResponse = await requestClient
.post(
url,
formData,
undefined,
'multipart/form-data; boundary=' + (formData as any)._boundary,
headers
)
.post(url, data, undefined, 'application/x-www-form-urlencoded', headers)
.then((res) => res.result as SasAuthResponse)
.catch((err) => {
throw prefixMessage(err, 'Error while getting access token. ')

View File

@@ -35,11 +35,12 @@ describe('getAccessTokenForViya', () => {
expect(requestClient.post).toHaveBeenCalledWith(
'/SASLogon/oauth/token',
expect.any(NodeFormData),
expect.any(URLSearchParams),
undefined,
expect.stringContaining('multipart/form-data; boundary='),
'application/x-www-form-urlencoded',
{
Authorization: 'Basic ' + token
Authorization: 'Basic ' + token,
Accept: 'application/json'
}
)
})