microsoft / kiota

OpenAPI based HTTP Client code generator
https://aka.ms/kiota/docs
MIT License
3.01k stars 210 forks source link

Error: Content type text/html does not have a factory registered to be parsed #4145

Closed naelob closed 9 months ago

naelob commented 9 months ago

Hey !

I've been stucked with an issue just after running npx ts-node index.ts (followed this guide https://learn.microsoft.com/en-us/openapi/kiota/tutorials/typescript-azure?tabs=portal)

I get the error: Error: Content type text/html does not have a factory registered to be parsed

Here is my repo : https://github.com/naelob/test-sdk

Capture d’écran 2024-02-06 à 13 55 02

Steps to reproduce:

kiota generate -l typescript -d api-doc.json -c Client -o ./client npx ts-node index.ts (of course I manually tried to use the sdk client by creating an index.ts file)

Thanks !

@baywet

baywet commented 9 months ago

Hi @naelob, This is most likely caused because you're initializing your client with an anonymous authentication provider when from reading the quick start of the API this API seems to require two authentication related headers:

Let us know if you have further questions.

naelob commented 9 months ago

Hi @naelob, This is most likely caused because you're initializing your client with an anonymous authentication provider when from reading the quick start of the API this API seems to require two authentication related headers:

  • the API key generated from their portal
  • the connection_token I recommend that you implement the authentication provider interface to set those two headers on the request information and pass an instance of that implementation to the request adapter.

Let us know if you have further questions.

Thank you for the answer. I actually built the api so only the api key is mandatory on certain routes (the / route is not protected). Connection token is used only at certain routes too. I still decided to check to implement a provider and found this https://learn.microsoft.com/en-us/openapi/kiota/authentication?tabs=typescript#:~:text=In%20getAuthorizationToken%2C%20the%20url%20parameter%20is%20the%20URL%20of%20the%20abstract%20request%20to%20be%20executed.%20The%20return%20value%20is%20a%20Promise%20that%20holds%20the%20access%20token%2C%20or%20null%20if%20the%20request%20could/should%20not%20be%20authenticated. I don't get it, I may have missed something but I'd like to use it like const authProvider = new BaseBearerTokenAuthenticationProvider('my_api_key');.

I have to build a specific url route so AccessTokenProvider can retrieve it through this url with getAuthorizationToken?

baywet commented 9 months ago

yes, the way this is designed:

  1. implement the AccessTokenProvider
  2. derive from BaseBearerTokenAuthenticationProvider in a new authentication provider class, this will pass your access token provider to the base.

You can check how AzureIdentity authentication and access providers work (same repo) to get a better idea.

naelob commented 9 months ago

AzureIdentity Thanks !

I still face the issue:

Capture d’écran 2024-02-06 à 17 10 55 Capture d’écran 2024-02-06 à 17 11 26
baywet commented 9 months ago

right, from reading the docs, I think this is only half the work here. You also need to set the connection_token request header.

Since this is really specific to this API, I suggest you :

  1. change your auth provider to implement authentication provider instead
  2. move the logic for the access token into the authentication provider, you can copy some of the logic from base bearer authentication provider to get the full authorization header
  3. add logic for the connection_token request header

With all those pieces in place, this should solve your authorization issue. If that still doesn't work, please get in touch with the API provider to get clarity on the authentication requirements

microsoft-github-policy-service[bot] commented 9 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.