glucas
glucas

Reputation: 1

How to correct an authentication issue in PowerQuery M Custom PowerBI Connector

I've built a custom PowerBI connector that can access a REST API. The connector works fine in the basic configuration, but fails when I try to add code to make a second API call to get additional metadata and rename columns.

I'm guessing this is a problem with me using PowerQuery incorrectly, but it's coming through with an authentication error problem.

The API uses username / password for initial login and then behind the scenes pass back an OAuth access token.

Working Code:

shared IE_to_PowerBI.Contents = (series_id as text, optional environment as text) =>

    let
        env = if environment is null then "prod" else "dev",
        url = if env = "dev" then dev_ie_api_url else prod_ie_api_url,
        token_url = if env = "dev" then dev_token_api_url else prod_token_api_url,

        creds = Json.FromValue([username = Extension.CurrentCredential()[Username], password = Extension.CurrentCredential()[Password]]),
        tokens = TokenMethod(creds, token_url),
        access_token = tokens[access_token],
        data = GetData(access_token, series_id),
    in 
        data;

Broken Code. It breaks when trying to add another call to the GetName function. The GetName function itself works fine when the GetData function isn't there:

shared IE_to_PowerBI.Contents = (series_id as text, optional environment as text) =>

    let
        env = if environment is null then "prod" else "dev",
        url = if env = "dev" then dev_ie_api_url else prod_ie_api_url,
        token_url = if env = "dev" then dev_token_api_url else prod_token_api_url,

        creds = Json.FromValue([username = Extension.CurrentCredential()[Username], password = Extension.CurrentCredential()[Password]]),
        tokens = TokenMethod(creds, token_url),
        access_token = tokens[access_token],
        data = GetData(access_token, series_id),
        name = GetName(access_token, series_id)
    in 
        data;

Error: The name 'Extension.CurrentCredential' wasn't recognized. Make sure it's spelled correctly.

The code works and downloads the needed data if I'm using the GetData function and passing out data OR using the GetName function and passing out name. But will not let me use both functions together.

Also, I'm using the Power Query SDK extension in VS Code. Is there an easy way where I can just store the username / password for future uses instead of clearing / setting them for every single test run?

Thanks.

Upvotes: 0

Views: 170

Answers (0)

Related Questions