Forum Discussion
Howe to get API data using Bearer Access Token dynamically?
- 3 years ago
I figured this out myself, and detailing here for someone else who might need it.
First, get the Access Token and convert it into a table:
let Source = Json.Document(Web.Contents("api-to-get-access-token")), #"Converted to Table" = Record.ToTable(Source), #"Sorted Rows" = Table.Sort(#"Converted to Table",{{"Name", Order.Descending}}), #"Promoted Headers" = Table.PromoteHeaders(#"Sorted Rows", [PromoteAllScalars=true]) in #"Promoted Headers"The sort rows and promote headers are just so I will have one row. The final result looks like this:
TABLE A:
Next, convert the original API call, which uses the access token value, into a function:
(token as text) => let Source = Csv.Document(Web.Contents("api-to-get-data", [Headers=[Authorization="Bearer " & token]]),[Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.None]), #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]) in #"Promoted Headers"Then, on the above TABLE A, invoke the custom function created (go to 'Add Column' tab on the menu, select 'Invoke Custom Function'), and select the newly created function, and select the value of the 'Bearer' column as the value to pass to the 'token'.
This will give you a new column, which you can expand to get the data.
Hi, can you set automatic refresh for the datasets with api dynamic access token ? It is not possible for me since the token expires and api accepts only "Anonymous" as data source credentials setting