Skip to main content
Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

Uniface 9: [SOLVED] Cosmos DB Emulator and Uniface

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

18 replies

Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

And here's the result from the call: vsResponse = "HTTP/1.1 200 Ok" vsHttpContent = "{"_rid":"","Databases":[{"id":"ToDoList","_rid":"QfBiAA==","_self":"dbs\\/QfBiAA==\\/","_etag":"\\"00000000-0000-0000-f1b9-b26f9bc401d3\\"","_colls":"colls\\/","_users":"users\\/","_ts":1526985798}],"_count":1}"


Author: diseli (daniel.iseli@uniface.com)
Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

Hi Thomas, This is working for me:

;——————————————————————– Variables   String vsUsername   String vsPassword   String vsUrl   String vsHttpHeader   String vsHttpContent   String vsResponse   String vsmaster   raw vsmasterraw   numeric vnHour   raw vsHashRaw   String vsVerb   raw vsKey   string vsSignature   datetime vsDatim   String vsDate   String vsResType   String vsResLinks EndVariables vsDatim = $datim vnHour=vsDatim[H]-2 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmasterraw=$decode("BASE64", vsmaster) vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%vsDatim[Aaa]%%%,%%% %%vsDatim[d]%%% %%vsDatim[Mmm]%%% %%vsDatim[y]%%% %%n99("%%vnHour")%%%:%%n99(vsdatim[n])%%%:%%n99(vsdatim[s])%%% GMT" vsHashRaw = $concat($encode("URAW", $lowercase(vsVerb)), $decode("HEX", "0A")) vsHashRaw = $concat(vsHashRaw, $encode("URAW", $lowercase(vsResType)), $decode("HEX", "0A")) vsHashRaw = $concat(vsHashRaw, $encode("URAW", vsResLinks), $decode("HEX", "0A")) vsHashRaw = $concat(vsHashRaw, $encode("URAW", $lowercase(vsDate)), $decode("HEX", "0A0A")) vskey=$encode("HMAC_SHA256",vshashraw,vsmasterraw) vsSignature=$encode("BASE64",vsKey) vsSignature=$encode("URL","type=master&ver=1.0&sig=%%vsSignature") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsSignature" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200)   message/info "%%vsHttpContent" Endif If ($Status=200 )   message/info "%%vsResponse" Endif return(0) ;——————————————————————– entry n99 returns string params   string pSrc : IN endparams   ; $N99$ => Display Format: DIS(99)   $N99$ = pSrc   return("%%$N99$") end

Please note that I had to adjust the time (my time zone is UTC+1). Hope this helps. Daniel


Author: diseli (daniel.iseli@uniface.com)
Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

Hi Daniel,  You are right but still i am getting the same error. I will investigate it further i'll come back. Anyway thanks a lot for your help.


Author: tommyb42 (tommy.balkis@gmail.com)
Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

Ok, this ” + Date.toLowerCase() + “ ” + “” + “ “; was a bit strange in your example. But you can probably just ignore + "" + as Daniel has done. The fact that Uniface encodes an empty string wrong, didn't have anything with your problem to do. Regards RogerW.


Author: rogerw (roger.wallin@abilita.fi)
Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

Hi Thomas, There are two issues here: 1. The $encode function expects the key as RAW and not BASE64 2. The default (internal) new line character is CR (0x0D) and not LF (0x0A) When I use the example encoding from the mentioned API reference page (https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources) then the following code will give the same result:

variables   string vVerb, vResourceType, vResourceLink, vDate, vKey, vKeyType, vTokenVersion   raw vKeyRaw, vPayLoadRaw, vHashPayLoad   string vSignature, vOutput endvariables vVerb = "GET" vResourceType = "dbs" vResourceLink = "dbs/ToDoList" vDate = "Thu, 27 Apr 2017 00:51:12 GMT" vKey = "dsZQi3KtZmCv1ljt3VNWNm7sQUF1y5rJfC6kv5JiwvW0EndXdDku/dkKBp8/ufDToSxLzR4y+O/0H/t4bQtVNw==" vKeyRaw = $decode("BASE64", vKey) vPayLoadRaw = $concat($encode("URAW", $lowercase(vVerb)), $decode("HEX", "0A")) vPayLoadRaw = $concat(vPayLoadRaw, $encode("URAW", $lowercase(vResourceType)), $decode("HEX", "0A")) vPayLoadRaw = $concat(vPayLoadRaw, $encode("URAW", vResourceLink), $decode("HEX", "0A")) vPayLoadRaw = $concat(vPayLoadRaw, $encode("URAW", $lowercase(vDate)), $decode("HEX", "0A0A")) vHashPayLoad = $encode("HMAC_SHA256", vPayLoadRaw, vKeyRaw) vSignature = $encode("BASE64", vHashPayLoad) vKeyType = "master" vTokenVersion = "1.0" vOutput = $encode("URL", $concat("type=", vKeyType, "&ver=", vTokenVersion, $concat("&sig=", vSignature)))

I hope this helps. Regards, Daniel


Author: diseli (daniel.iseli@uniface.com)
Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

Hi, There is not a problem with the encoding, except if you try to encode an empty/null string. Otherwise we have got the same result as with VS. Ok, now I saw what you meant. Strange.... You probably have to hardcode the "", but I'm not sure what + "" + "" really mean..... Regards RogerW.


Author: rogerw (roger.wallin@abilita.fi)
Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

Hi Roger ,  If there is a problem with the "" for  HMAC_SHA256 encoding  perhaps this is a probably answear beacause according the instructions you have to pass this to the key  StringToSign = Verb.toLowerCase() + " " + ResourceType.toLowerCase() + " " + ResourceLink + " " + Date.toLowerCase() + " " + "" + " ";


Author: tommyb42 (tommy.balkis@gmail.com)
Jan Cees Boogaard
Rocketeer
Nov 19, 2018 11:39 AM UTC

[SOLVED] Cosmos DB Emulator and Uniface

Author: tommy.balkis@gmail.com (tommyb42)

Hi all ,  I am trying to develop a Restfull Api call with Uniface  to microsoft Cosmos Db emulator. They have a complex  Authorization token which i construct it  according to microsft 's instructions. I'm always getting an error from the token validation from server but i cannot find a possible reason. I make the same call from visual studio with .Net Sdk and it works fine  Please find below the uniface code for the http call  ;-------------------------------------------------------------------- Variables String vsUsername String vsPassword String vsUrl String vsHttpHeader String vsHttpContent String vsResponse String vsmaster numeric vnHour String vsHash String vsVerb String vsKey String vsDate String vsResType String vsResLinks EndVariables vnHour =$datim[H]-3 vsmaster="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==" vsmaster =$decode("BASE64", "%%vsmaster") vsVerb="GET" vsResType="dbs" vsResLinks ="" vsDate ="%%$datim[Aaa]%%%,%%% %%$datim[d]%%% %%$datim[Mmm]%%% %%$datim[y]%%% %%FixSize("%%vnHour ",2,"0",1)%%%:%%FixSize("%%$datim[n]",2,"0",1)%%%:%%FixSize("%%$datim[s]",2,"0",1)%%% GMT" vsHash ="%%$Lowercase(vsVerb)%%^%%$lowercase(vsResType)%%^%%vsResLinks%%^%%$Lowercase(vsDate)%%^%%% %%^" vskey=$encode("HMAC_SHA256","%%vshash","%%vsmaster") vskey=$encode("BASE64","%%vsKey") vskey=$encode("URL","type=master&ver=1.0&sig=%%vskey") NewInstance "UHTTP" , "CosmosDb" Activate "CosmosDb".Set_Flags(10) vsResponse = "" vsUrl = "https://localhost:8081/dbs" putitem/id/case vsHttpHeader, "Authorization","%%vsKey" putitem/id/case vsHttpHeader, "x-ms-date","%%vsDate" putitem/id/case vsHttpHeader,"Content-Type", "application/json;charset=UTF-8" putitem/id/case vsHttpHeader, "x-ms-version","2017-02-22" Activate "CosmosDb".send(vsUrl, %\\ "GET", %\\ vsUsername, %\\ vsPassword , %\\ vsHttpHeader, %\\ vsHttpContent, %\\ vsResponse) If ($Status !=200) message/info "%%vsHttpContent" Endif If ($Status=200 ) message/info "%%vsResponse" Endif ;-------------------------------------------------------------------- I am using uniface 9.7.1 and i also run it from uniface 9.7.3 You can download the emulator from here : https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator and  the Api Referece from here :https://docs.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources   I would appreciate every help to make it  work  from uniface.   Regards Thomas

Hi Knut ,  The error message is : "{"code":"Unauthorized","message":"The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'get dbs tue, 22 may 2018 7 :47:08 gmt ' ActivityId: c33075ef-a447-4cae-b803-a24a62b60d8e, Microsoft.Azure.Documents.Common/1.20.108.4"}"


Author: tommyb42 (tommy.balkis@gmail.com)