swagger-api / swagger-api/swagger-codegen
[C# Swagger generated rest client] Problem authorizing requests via the generated client.
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Hey,
Sorry in advance, not sure this is the right place to post this kind of problem.
I'm working on a MVC web project where someone else had wrote all the api controllers and done most of the setup. I was asked to try get these controllers to work with the auto generated rest client provider by swagger.
So I have a number of API controllers in a main project. I used (websiteUrl)/Swagger/docs/v1, to get the .json description of the API and then used the swagger online editor tools to generate a C# Rest client for me.
I first called the auth token api as below with the correct username and password which returns me a token and token type of bearer as below:
var authApi = new ApiAuthTokenApi();
var auth = authApi.ApiAuthToken("password", userName, password);
I add this token and token type of Bearer to the Configuration object that I later pass into the constructor of the api class as below:
clientConfig.ApiKey["Authorization"] = response.AccessToken;
clientConfig.ApiKeyPrefix["Authorization"] = "Bearer";
I then make the sample request to one of the api services as below:
var instance = new ApiAddressApi(clientConfig);
var address = instance.ApiAddressFind(7);
It then hits the below bit of generated code for one of the services:
// make the HTTP request
IRestResponse localVarResponse = (IRestResponse) Configuration.ApiClient.CallApi(localVarPath,
Method.GET, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams,
localVarPathParams, localVarHttpContentType);
My apiKey and apiKeyPrefix are populated as expected on the Configuration.ApiClient object used in the above few lines of code, but it still returns as unauthorized.
Any ideas?
Thanks!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated C# client's Configuration.ApiClient.CallApi path and inspect how ApiKey and ApiKeyPrefix are applied to the Authorization header. Reproduce the ApiAddressFind(7) request with the shown token setup and compare the emitted authorization data with the API's expected bearer authorization. Done means the protected request is accepted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100