WinHttp 5.1 oauth2 authentication Add Bearer Token to the header

mbugua699
Member Posts: 2
Hi all,
I want to use WinHttp.SetRequestHeader('Authorization', "Bearer xyzACCESS_Token[952 Characters]_xyz") to send access token, but I get Message 401(Unauthorized).
The token expiry is 150s so there is enough time to send it. The examples available on forum are using Basic Authentification but I cannot use basic
in my scenario.
The length of the access token is 952.
Am using AUTOMATION 'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest
Could there be anything am missing or is there a different approach to send the Bearer token?
My code :
WinHttp.Open('POST', url, FALSE);
WinHttp.SetRequestHeader('Accept', 'application/json');
WinHttp.SetRequestHeader('Accept-Language', 'en_US');
WinHttp.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
WinHttp.SetRequestHeader('Authorization', AccessToken);
WinHttp.Send(NodeText.nodeValue);//sending Json
I want to use WinHttp.SetRequestHeader('Authorization', "Bearer xyzACCESS_Token[952 Characters]_xyz") to send access token, but I get Message 401(Unauthorized).
The token expiry is 150s so there is enough time to send it. The examples available on forum are using Basic Authentification but I cannot use basic
in my scenario.
The length of the access token is 952.
Am using AUTOMATION 'Microsoft WinHTTP Services, version 5.1'.WinHttpRequest
Could there be anything am missing or is there a different approach to send the Bearer token?
My code :
WinHttp.Open('POST', url, FALSE);
WinHttp.SetRequestHeader('Accept', 'application/json');
WinHttp.SetRequestHeader('Accept-Language', 'en_US');
WinHttp.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
WinHttp.SetRequestHeader('Authorization', AccessToken);
WinHttp.Send(NodeText.nodeValue);//sending Json
0
Best Answer
-
I was working on this the other day, Not sure about the WinHTTP services, I used System.Net.Http
My approach was the followingLOCAL CallRESTWebService(BaseUrl : Text;Method : Text;RestMethod : Text;HttpContent : DotNet "System.Net.Http.HttpContent";VAR HttpResponseMessage : DotNet "System.Net.Http.HttpResponseMessage") HttpClient := HttpClient.HttpClient(); HttpClient.DefaultRequestHeaders.Authorization := AuthenticationHeaderValue.AuthenticationHeaderValue('Bearer',GetToken); MediaTypeWithQualityHeaderValue := MediaTypeWithQualityHeaderValue.MediaTypeWithQualityHeaderValue('application/json'); HttpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue); CASE RestMethod OF 'GET' : HttpResponseMessage := HttpClient.GetAsync(Method).Result; 'POST' : HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result; 'PUT' : HttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result; 'DELETE' : HttpResponseMessage := HttpClient.DeleteAsync(Method).Result; END; HttpResponseMessage.EnsureSuccessStatusCode();
Works for me5
Answers
-
Your method should work.
Try to use "Uri" method instead of headers. Include "access_token=XXXXXXXXXXXXXXXXXXXX" in your request address - "https://your-address.com/something?access_token=XXXXXXXXXX&something_else=1"0 -
Hello @mbugua699,
Just a question in the line below the variable AccessToken is 'Bearer ' + the acces token, right ?WinHttp.SetRequestHeader('Authorization', AccessToken);
Regards.0 -
I was working on this the other day, Not sure about the WinHTTP services, I used System.Net.Http
My approach was the followingLOCAL CallRESTWebService(BaseUrl : Text;Method : Text;RestMethod : Text;HttpContent : DotNet "System.Net.Http.HttpContent";VAR HttpResponseMessage : DotNet "System.Net.Http.HttpResponseMessage") HttpClient := HttpClient.HttpClient(); HttpClient.DefaultRequestHeaders.Authorization := AuthenticationHeaderValue.AuthenticationHeaderValue('Bearer',GetToken); MediaTypeWithQualityHeaderValue := MediaTypeWithQualityHeaderValue.MediaTypeWithQualityHeaderValue('application/json'); HttpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue); CASE RestMethod OF 'GET' : HttpResponseMessage := HttpClient.GetAsync(Method).Result; 'POST' : HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result; 'PUT' : HttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result; 'DELETE' : HttpResponseMessage := HttpClient.DeleteAsync(Method).Result; END; HttpResponseMessage.EnsureSuccessStatusCode();
Works for me5 -
I also see that you have Content-Type x-www-form-urlencoded, I ran into some problems getting the Bearer Token, don't know if this helps you.
https://forum.mibuso.com/discussion/75345/generic-list-and-x-www-form-urlencoded-rest-api#latest0 -
@ftornero Yes the variable AccessToken := "Bearer + the acces token" . I have used @wait method and was able to send the Bearer token. @BlackTiger thanks for reply, but I tried to add it to the url header but it did not work, maybe due to the specific api server.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