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

Authentication for APIs with basic authentication and bearer #3230

Open
robertvanmolken opened this issue Aug 8, 2023 · 4 comments
Open
Assignees

Comments

@robertvanmolken
Copy link

I'm looking for an example implementation that can call APIs protected by either basic auth as bearer token.

I see that there is a BaseBearerToken provider but can find any example implementation.

It requires an AccessTokenProvider and I am struggling how to implement that.

Can this please be added to the documentation

@baywet
Copy link
Member

baywet commented Nov 15, 2023

Hi @robertvanmolken
Thank you for using kiota and sorry for the delay in the answer, it appears I was not watching issues on this repository.
Which language are you referring to?

@baywet baywet self-assigned this Nov 15, 2023
@jakejscott
Copy link

Examples for dotnet would be great :)

@baywet
Copy link
Member

baywet commented Jan 30, 2024

provided a bit of guidance, would either of you be willing to submit a pull request?

@pickettd
Copy link

pickettd commented Feb 9, 2024

@robertvanmolken and @jakejscott - I'll paste a snippet below that worked for me for using Kiota with bearer auth in dotnet. And @baywet I'm willing to submit a PR if it is useful

using KiotaPosts.Client.Models;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;

// authentication provider using class from below
TokenProvider myTokenProvider = new TokenProvider();
var authProvider = new BaseBearerTokenAuthenticationProvider(myTokenProvider);
// Create request adapter using the HttpClient-based implementation
var adapter = new HttpClientRequestAdapter(authProvider);
// Create the API client
var client = new Client(adapter);

try
{
    var findId = "<example_cust_id>";

    var idCust = await client.Customers[findId].GetAsync();
    Console.WriteLine($"Retrieved by id: {idCust.Customer.Email}");
    Console.WriteLine($"Retrieved by id: {idCust.Customer.Id}");
}
catch (Exception ex)
{
    Console.WriteLine($"ERROR: {ex.Message}");
    Console.WriteLine(ex.StackTrace);
}

internal class TokenProvider : IAccessTokenProvider
{
    public AllowedHostsValidator AllowedHostsValidator { get; }

    public Task<string> GetAuthorizationTokenAsync(
        Uri uri,
        Dictionary<string, object>? additionalAuthenticationContext = null,
        CancellationToken cancellationToken = default)
    {
        return Task.FromResult("<insert_bearer_token>");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants