WinHttp 5.1 oauth2 authentication Add Bearer Token to the header

mbugua699mbugua699 Member Posts: 2
edited 2020-06-17 in NAV Three Tier
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

Best Answer

Answers

  • BlackTigerBlackTiger Member Posts: 2
    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"
  • ftorneroftornero Member Posts: 310
    Hello @mbugua699,

    Just a question in the line below the variable AccessToken is 'Bearer ' + the acces token, right ?
    WinHttp.SetRequestHeader('Authorization', AccessToken);
    

    Regards.
  • waitwait Member Posts: 43
    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#latest
  • mbugua699mbugua699 Member Posts: 2
    @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.
Sign In or Register to comment.