Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dvdbroek
Frequent Visitor

Using the given variable parameters and putting them in a JSON format body in M

Dear PowerBI wizzards,

 

Im pretty new to M and its coding. Currently im working to make a presentation with its input comes from an API. The API itselfs needs to be provided with a ClientIdentifier and ClientSecret. When this is done it, it returns me an API acces key wich i then can use to get the data that i want.

 

The problem that im running into is that i need to get data from multiple clouddirectorys. These different directorys all have differnt credentials.

 

This is my M code to call the API to give me the API Acces Key that i need:

 

 

 

 

//(UserName as text, OfficeCode as text, Password as text)=>

let
    url = "https://xxxxxxxxxxxxxxx",
   body = 
    "{""clientIdentifier"":""XXXXXXXX"",
        ""clientSecret"":""XXXXXXXX"",
        ""username"": ""XXXXX"",
        ""password"": ""XXXX"",
        ""officeCode"": ""XXXXXX"",
        
        
        }",
 
    Source = Json.Document(Web.Contents(url,[

           Headers = [#"Content-Type"="application/json"],

           Content = Text.ToBinary(body)] ))
in
    Source

 

 

 

 

In the JSON body i hardcoded the credentials need to provide me the acces key. Now i want to re-use the code above to give me an acces key for every directory but im failing to combine the variables into the JSON body. Im looking for someting like this:

 

body = "{""username"": UserName}".

 

So my question is how can i implement the given paratamers in the top of the code commented out into my JSON body?

 

Much thanks in advance already!!!

1 ACCEPTED SOLUTION
dvdbroek
Frequent Visitor

I came up with this and it works:

 

 body = "{""clientIdentifier"":""xxxxxxxx"",
        ""clientSecret"":""xxxxxxxx"",
        ""username"":" & Text.FromBinary(Json.FromValue(UserName)) & "," & "
        ""password"":" & Text.FromBinary(Json.FromValue(Password)) & "," & "
        ""officeCode"":" & Text.FromBinary(Json.FromValue(OfficeCode)) & "}",

 

If there are smarter or better solutions i gladly hear them:) 

View solution in original post

1 REPLY 1
dvdbroek
Frequent Visitor

I came up with this and it works:

 

 body = "{""clientIdentifier"":""xxxxxxxx"",
        ""clientSecret"":""xxxxxxxx"",
        ""username"":" & Text.FromBinary(Json.FromValue(UserName)) & "," & "
        ""password"":" & Text.FromBinary(Json.FromValue(Password)) & "," & "
        ""officeCode"":" & Text.FromBinary(Json.FromValue(OfficeCode)) & "}",

 

If there are smarter or better solutions i gladly hear them:) 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors