Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JavaScript] How do I set auth headers for all/some requests? #6564

Closed
dganoff opened this issue Sep 25, 2017 · 3 comments
Closed

[JavaScript] How do I set auth headers for all/some requests? #6564

dganoff opened this issue Sep 25, 2017 · 3 comments

Comments

@dganoff
Copy link

dganoff commented Sep 25, 2017

Description

I generated a JavaScript SDK, but am not clear on how to set authentication headers. I'd love to be able to set the auth headers used for all future requests via some sort of default headers setting. I've tried setting the ApiClient.defaultHeaders like below, but with no success:

Update: I updated the below code showing a way I got the headers to be added to the requests made by connAPI, but this code is definitely less than ideal. There must be another way to set these auth headers in a more flexible/programmatic way.

import telnyxSDK from 'telnyx-sdk';

const connAPI = new telnyxSDK.ConnectionsApi();
connAPI.apiClient.defaultHeaders = {
  'x-api-user': 'my-username',
  'x-api-token': 'my-token'
};

let _payload = {
  connection_name: 'test-name',
  tech_prefix: 'test-prefix',
  active: true
};

return connAPI.addConnection(_payload).then(function(data) {
  console.log('API called successfully. Returned data: ' + data);
}, function(error) {
  console.log(error);
});

The request gets made, but without any of the auth headers.

Please advise, thanks!

Swagger-codegen version

2.2.3 (via swagger-codegen-cli docker image)

Swagger declaration file content or url

https://api.telnyx.com/mission_control_docs

Command line used for generation

swagger-codegen generate -i https://api.telnyx.com/mission_control_docs -l javascript

@wing328
Copy link
Contributor

wing328 commented Oct 3, 2017

@dganoff if your openapi/swagger spec contains the proper security definition for endpoints, then the auto-generated documentation should contain the code samples on how to make the proper call.

Did you have a chance to review the auto-generated doc in the JS API client?

@dganoff
Copy link
Author

dganoff commented Oct 4, 2017

@wing328 Yes, I looked over the documentation for each <Name>Api.md and they all have a section for "Authorization" that reads "No authorization required". I'm assuming this is where it would document how to set the auth headers.

I'm wondering if the operations from the paths in our swagger spec need to have a security key in them?

@wing328
Copy link
Contributor

wing328 commented Oct 4, 2017

Yup, you will need to add security to the endpoint definition, e.g. https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L183

@wing328 wing328 closed this as completed Dec 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants