httpyac / httpyac.github.io

Documentation and Examples for httpyac
https://httpyac.github.io
MIT License
105 stars 9 forks source link

Question: how to insert oauth2Session.accessToken into the body? #63

Closed nikolay-turpitko closed 1 year ago

nikolay-turpitko commented 1 year ago

I'm trying to do the following:

POST https://clientcenter.api.bingads.microsoft.com/Api/CustomerManagement/v13/CustomerManagementService.svc
Authorization: oauth2 code
Content-Type: text/xml
SOAPAction: GetUser

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v13="https://bingads.microsoft.com/Customer/v13">
<soapenv:Header>
   <v13:DeveloperToken>{{devToken}}</v13:DeveloperToken>
   <v13:AuthenticationToken>{{oauth2Session.accessToken}}</v13:AuthenticationToken>
</soapenv:Header>
<soapenv:Body>
   <v13:GetUserRequest>
      <v13:UserId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
   </v13:GetUserRequest>
</soapenv:Body>
</soapenv:Envelope>

{{
  console.info(oauth2Session);
}}

oauth2Session is logged OK (if I remove <v13:AuthenticationToken> line), but it gives error on attempt to use it in the mentioned line. Without this SOAP header I get an auth error from the remote server. Is there any way to fix it?

AnWeber commented 1 year ago

There is an order problem in my program. Currently the variables in the body are replaced first and then the header variables. But only by replacing the Authorization Header the variable oauth2Session is defined. I did not consider this case that in the body also the token could be needed. I'm adjusting the order because it makes more sense that way.

nikolay-turpitko commented 1 year ago

Thx for your quick replay and Happy New Year!

On the topic: I'm not sure, but if it is not straightforward how to fix it, maybe it's possible to create some ugly workaround? For instance, I tried to use OPTIONS request to obtain headers first and then pass oauth2Session to the next request. But I was not able to fill global variable from postscript block of OPTIONS request. Maybe I missed something.

AnWeber commented 1 year ago

Happy New Year to you too:-)

Yes, there is already a workaround. The approach with the OPTIONS request is good. The reason is probably that I have inserted an explicit variable scoping and you have to explicitly reference variables of other areas (see @ref. This should work (not tested).

# @name token

OPTIONS https://clientcenter.api.bingads.microsoft.com/Api/CustomerManagement/v13/CustomerManagementService.svc
Authorization: oauth2 code
...

{{
  exports.token = oauth2Session.accessToken;
}}
...
###
# @ref token
POST https://clientcenter.api.bingads.microsoft.com/Api/CustomerManagement/v13/CustomerManagementService.svc
Authorization: Bearer {{token}}
Content-Type: text/xml
SOAPAction: GetUser

...
AnWeber commented 1 year ago

I released a version with v5.9.0 where the original request should work. Have fun trying it out

nikolay-turpitko commented 1 year ago

Works like a charm, thanks!