1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-08 04:50:06 +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 { SasAuthResponse } from '@sasjs/utils/types'
import { prefixMessage } from '@sasjs/utils/error' import { prefixMessage } from '@sasjs/utils/error'
import * as NodeFormData from 'form-data'
import { RequestClient } from '../request/RequestClient' import { RequestClient } from '../request/RequestClient'
/** /**
@@ -24,26 +23,17 @@ export async function getAccessTokenForViya(
token = Buffer.from(clientId + ':' + clientSecret).toString('base64') token = Buffer.from(clientId + ':' + clientSecret).toString('base64')
} }
const headers = { const headers = {
Authorization: 'Basic ' + token Authorization: 'Basic ' + token,
Accept: 'application/json'
} }
let formData const data = new URLSearchParams({
if (typeof FormData === 'undefined') { grant_type: 'authorization_code',
formData = new NodeFormData() code: authCode
} else { })
formData = new FormData()
}
formData.append('grant_type', 'authorization_code')
formData.append('code', authCode)
const authResponse = await requestClient const authResponse = await requestClient
.post( .post(url, data, undefined, 'application/x-www-form-urlencoded', headers)
url,
formData,
undefined,
'multipart/form-data; boundary=' + (formData as any)._boundary,
headers
)
.then((res) => res.result as SasAuthResponse) .then((res) => res.result as SasAuthResponse)
.catch((err) => { .catch((err) => {
throw prefixMessage(err, 'Error while getting access token. ') throw prefixMessage(err, 'Error while getting access token. ')

View File

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