Bearer Authorization does not work

c_miesenberger
Member Posts: 5
I am currently working on an API with GraphQL that uses Bearer tokens for authentication. It's strange because it always works in Postman, but when I try it in Business Central, it doesn't.
It seems like the API doesn't recognize that I want to use Bearer tokens for authentication because I always get some HTML code as a response.
Here is my code:

It seems like the API doesn't recognize that I want to use Bearer tokens for authentication because I always get some HTML code as a response.
Here is my code:

0
Answers
-
Hello @c_miesenberger,
I never use the DefaultRequestHeaders, you can try something like this that works for meRequestHeader.Clear(); HttpRequestMessage.GetHeaders(RequestHeader); RequestHeader.Add('Authorization', 'Bearer ' + LunarAPI.GetAccessToken()); ...............
Where RequestHeader isRequestHeader: HttpHeaders;
Regards.0 -
Unfortunately, that didn't work either; the same error keeps occurring. However, while debugging, I noticed that the RequestUri of the RequestMessage changes after HttpClient.Send(). Maybe it has something to do with that?0
-
This piece of code works with Bearer tokens, perhaps you can make use of something like this:
internal procedure CallAPI(): text var OccuredErr: Label 'An error occured: %1'; Client: HttpClient; header: HttpHeaders; Response: HttpResponseMessage; Request: HttpRequestMessage; respTxt: text; begin Request.GetHeaders(header); header.Add('Authorization', 'Bearer ' + GetAccessToken()); Request.Method('GET'); request.SetRequestUri(BuildURL()); Client.Send(Request, Response); if Response.IsSuccessStatusCode then begin Response.Content.ReadAs(respTxt); //Handle Reponse here end else Error(OccuredErr, GetLastErrorText()) end;
For help, do not use PM, use forum instead, perhaps other people have the same question, or better answers.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions