gpt4 book ai didi

c# - OAuth2.0如何与OData Client Code Generator集成?

转载 作者:可可西里 更新时间:2023-11-01 08:57:30 24 4
gpt4 key购买 nike

我开发了一个实现 OAuth2.0 和 OData 的 WebApi。

现在我正在创建一个客户端来测试我到目前为止实现的内容。我已经使用 OData 客户端代码生成器生成了 OData 模板,但如何在 OData 请求中引入去访问 token ?

知道如何扩展 OData 模板以引入 OAuth2.0 方案吗?或者更简单的方法,我如何在每个 OData 请求中引入 OAuth 访问 token ?

更新

static void Main(string[] args)
{
var container = new Default.Container(new Uri(baseurl));

TokenResponse accessToken = null;
try
{
accessToken = GetToken();
}
catch (Exception ex)
{
Console.WriteLine("Can't do nothing without an access token...");
return;
}


//I want to introduce in every request the following information:
//Basic autentication header with cliendId + clientSecret
//Access token

//How do I introduce them before making the call on the OData service?
foreach (var s in container.ServiceSessions)
{
string format = ";
Console.WriteLine("PKID:{0}", s.PKID);
}
}

最佳答案

经过一些研究,我找到了解决方案:

var container = new Default.Container(new Uri(http://localhost:9000/));

//Registering the handle to the BuildingRequest event.
container.BuildingRequest += (sender, e) => OnBuildingRequest(sender, e, accessToken);


//Every time a OData request is build it adds an Authorization Header with the acesstoken
private static void OnBuildingRequest(object sender, BuildingRequestEventArgs e, TokenResponse token)
{
e.Headers.Add("Authorization", "Bearer " + token.AccessToken);
}

关于c# - OAuth2.0如何与OData Client Code Generator集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27798538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com