简体   繁体   English

C#:如何在 header 中使用不记名令牌获取请求

[英]C#: How to htttps get request with bearer token in header

Hello everyone my name is Taniguchi.大家好,我的名字是谷口。

I've tried retrieving an entity from Dynamics 365 Finance and Operations and i did it on postman but now i having difficulties in asp.net core I was able to get the token but when i try to retrieve the entity the response gives me a html and this html leads me to the Dynamics 365 Finance and Operations Page.我尝试从 Dynamics 365 Finance and Operations 中检索实体,我在 postman 上进行了操作,但现在我在 asp.net 核心中遇到了困难html 将我带到 Dynamics 365 Finance and Operations 页面。 My code: `我的代码:`

        var httpClient = new HttpClient();

        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

        const string environmentsUri = "https://trial-0z4qfj.sandbox.operations.dynamics.com/data/PartyContacts";

        var response = httpClient.GetAsync(environmentsUri).Result;

        var content = response.Content.ReadAsStringAsync().Result;

what am i doing wrong?我究竟做错了什么? do i need to authenticate again?我需要再次认证吗?

I suspect the Token you receive is not correct/complete.我怀疑您收到的令牌不正确/不完整。 I remember I faced the similar issue, I would request you to follow this blog post and try to receive Token as shown This shall help you solve your issue.我记得我遇到过类似的问题,我会要求您关注这篇博文并尝试接收如图所示的令牌这将帮助您解决您的问题。

In addition I would recommend you use Organizaiton service with Webapi/Access Token.此外,我建议您使用带有 Webapi/Access Token 的 Organizaiton 服务。 It will give you native library usage and you could query crm easily.它将为您提供本机库的使用,您可以轻松查询 crm。

Try this blog for Orgservice 试试这个关于 Orgservice 的博客

Seems like you are only using the parameter accessToken instead of the variable value.好像您只使用参数accessToken而不是变量值。 Depends on how you declared accessToken ofc.取决于您如何声明accessToken ofc。 The rest seems fine. rest 看起来不错。

Try:尝试:

httpClient.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", token.accessToken);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Bearer 令牌向 api 发出 Get 请求并将响应数据与 C# (Windows Server)一起使用? - How do you make a Get request to api with Bearer token and use the response data with C# (Windows Server)? C# 将不记名令牌传递给生成的代理 class 以添加 header - C# Passing bearer token to generated proxy class to add header 如何在HTTP请求的标头中添加承载令牌? - How do I add a Bearer Token to the header of a HTTP request? 使用 Bearer Token 在 C# 中构建 post HttpClient 请求 - Building post HttpClient request in C# with Bearer Token 如何诊断尝试在 c# .NET Core 中获取 OAuth2 不记名令牌的 401 错误? - How to diagnose a 401 error attempting to get an OAuth2 bearer token in c# .NET Core? 如何在 C# 中为 Google AutoML 获取 OAuth 2.0 身份验证承载令牌? - How to get an OAuth 2.0 authentication bearer token in C# for Google AutoML? c# - 如何在c#中使用“授权”、“承载”、令牌创建指向URL的websocket - How to create a websocket to a URL using "Authorization", "Bearer", token in c# 使用C#的OAuth Bearer令牌实现 - OAuth Bearer token implementation using C# 在c#中手动解码OAuth承载令牌 - Manually decode OAuth bearer token in c# 使用 C# 生成不记名令牌 - Generate bearer token using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM