Search code examples
excelpowerquerydata-analysisdata-cleaningm

Power Query: Web.Content request fails with dynamic property values


I am reading data from a REST API using Power Query. this is my request:

let
    url = "https://example",
    token = "MyToken",
    options = [Headers=[Authorization="Bearer " & token]],
    contents = Web.Contents(url, options)
in
    contents

I have to authorise via bearer token, which works like a charm with the above code.

However if I change my code slightly:

This part is resolved by Davide's answer, it works with the uppercase From

let
    url = "example",
    token = Text.from("MyToken"),
    options = [Headers=[Authorization="Bearer " & token]],
    contents = Web.Contents(url, options)
in
    contents

the Web.Contents request fails with an

[Expression.Error] The import Text.from matches no exports. Did you miss a module reference?

Another option I tried is to read from a cell:

let
    url = "https://example",
    token = Excel.CurrentWorkbook(){[Name="MyToken"]}[Content]{0}[Column1],
    options = [Headers=[Authorization="Bearer " & token]],
    contents = Web.Contents(url, options)
in
    contents

In this case the Web.Contentrequest fails with

An on-premises data gateway is required to connect.

The only difference here is, that in the first case token is a static string, while in both the adjusted code examples it is a dynamic string, because it now is the output of a runtime function.

I have to read the token from an external source, so keeping it as static string in the script is not an option.

Any idea how to make this work would be appreciated.


Solution

  • Found an answer by chance:

    1. Launch the Power Query Editor
    2. On the Home tab press the 'Options' dropdown > Project options
    3. Make sure the "Allow combining data from multiple sources" checkbox is checked. Please make sure you also read the whole description - "This could expose sensitive or confidential data to an unauthorized person".

    Source: https://answers.microsoft.com/en-us/msoffice/forum/all/excel-power-query-an-on-premises-data-gateway-is/6ac3292d-d710-4fd2-8cfa-999ec8553157