Forum Discussion

edubcardoso's avatar
edubcardoso
Advocate I
3 years ago
Solved

Get data from API with token that expires

Hey there. I hope you are all ok. I want to get data from an API. I was able to connect to Postman, but I can't connect to Power BI.   First i have to use the POST method, with no Authorization an...
  • edubcardoso's avatar
    edubcardoso
    3 years ago

    I just solved it.

     

    Funciton GetAcessToken

    () =>
    let
    Url = "https://api.prismart.pt/api/Auth/login",
    Body ="{ ""username"": ""xxxx"", ""password"": ""xxxx""}",
    // Define the POST Request.
    Source = Json.Document(
    Web.Contents(
    Url,
    [
    Headers=[#"Content-Type"="application/json"],
    Content= Text.ToBinary(Body)
    ]
    
    
    )
    ),
    JsonOutput = Json.FromValue(Source),
    Text = Text.FromBinary(JsonOutput)
    
    
    in
    
    
    Text

    And

    let
    Source = Json.Document(Web.Contents("https://api.prismart.pt/api/1040/InferenceDocuments/filter?taxonomy=Fatura&start=2022-08-01&end="&end, [Headers=[Authorization="Bearer "& GetAcessToken() ]])),
        #"Convertido em Tabela1" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Column1 Expandida" = Table.ExpandRecordColumn(#"Convertido em Tabela1", "Column1", {"id", "document", "type", "name", "state", "date", "documentId", "resultJson", "successPercentage", "taxonomy", "createdBy", "createdByName", "createdOn", "companyId", "errorMessage"})
    in
        #"Column1 Expandida"

     

    Thanks for the help v-kkf-msft 
    I hope this page can help someone in the future.