Sending bearer token to autorest typescript client
Sending bearer token to autorest typescript client
I am using autorest to generate TypeScript clients for accessing RESTful web services. All REST api looks for bearer token for authentication but I didn't find a way to pass token to autogenerated TypeScript clients.
I did try searching autorest documentation. It looks like I need to use ServiceClientCredentials but I didn't find any sample code.
Does anyone know how to use ServiceClientCredentials in TypeScript?
I use the following command to generate TypeScript clients
autorest --input-file=restapi.json --typescript --output-folder=./output --package-name="test-api" --package-version="0.1.0" --generate-metadata=true --add-credentials=true
1 Answer
1
I found the answer. I need to create an object of TokenCredentials
and pass it in autogenerated client
TokenCredentials
// Create token
const tokenCredentials: TokenCredentials = new TokenCredentials(
'some token'
);
// Add token and server url to service instance
const service: AutoGeneratedSvc = new AutoGeneratedSvc(
tokenCredentials,
'server url'
);
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.