Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Please Help: Trying to encode/decode Access Token (Rest API)

Hi Folks,   I'm new to Power BI and was hoping I could ask someone over here for some advice.  I'm working with the Rest API.  And well, I've got most of what I need working within the Power Query ...
  • Anonymous's avatar
    Anonymous
    6 years ago

    For those curious this eventually was determined to be a server bug.  However, I did clean up the final code.  Attached is the working code for anyone that wants it...

    (environment as text) as text =>
    
    let
        sand_UserID = [REMOVED],
        sand_Database = [REMOVED],
        sand_Password = Uri.EscapeDataString([REMOVED]),
        sand_Client_Secret = Uri.EscapeDataString([REMOVED]),
    //EscapeDataString is required for any phrase that includes special characters, although probably doesn't hurt to have on all of them.
      
        prod_UserID = [REMOVED],
        prod_Database = [REMOVED],
        prod_Password = Uri.EscapeDataString([REMOVED]),
        prod_Client_Secret = Uri.EscapeDataString([REMOVED]),
    
        Client_ID = Uri.EscapeDataString([REMOVED]),
    
        UserID = if environment = "sand" then sand_UserID else prod_UserID,
        Database = if environment = "sand" then sand_Database else prod_Database,
        Password = if environment = "sand" then sand_Password else prod_Password,
        Client_Secret = if environment = "sand" then sand_Client_Secret else prod_Client_Secret,
    
    
            token_url = "https://[REMOVED]/api/",
    
            body="Username="&UserID&"&Password="&Password&"&grant_type=password&Integrated=N&database="&Database&"&Client_Id=" & Client_ID & "&Client_Secret=" & Client_Secret,
    
            params =
            [ 
                Content = Text.ToBinary(body),
                RelativePath = "Token",
                Headers = [#"Content-Type"="application/x-www-form-urlencoded"]
            ],
    
            WebSource = Json.Document(Web.Contents(token_url,params)),
    
            apikey = WebSource[access_token]
    in
        apikey