Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in BI by (17.6k points)

I am building a custom connector to connect to our API via OAuth2. This is so we can use our api as a data source to Power Bi.

// Resource definition

        Resource = [

            Description = "MyAPI",

            Type = "Custom",

            MakeResourcePath = (env) => env,

            ParseResourcePath = (env) => {env},

            Authentication = [OAuth=[StartLogin = StartLogin, FinishLogin = FinishLogin, Refresh = Refresh]],

    ......

Icons = [

            Icon16 = { Extension.Contents("MyAPI10.png"), Extension.Contents("MyAPI20.png") }

        ],

        Label = "MyAPI"

    ]

in

    Extension.Module("MyAPI", { Resource })

I used MakeResourcePath and ParseResourcePath to pass the Environment parameter (which is taken as input from the user in power bi site/desktop). This is passed to StartLogin to make the OAuth authorize call.

 StartLogin = (env, state, display) =>

        let

            resourceUrl = getOAuthUrlFromEnvName(env) & "/oauth/authorize",

            AuthorizeUrl = resourceUrl & "?" & Uri.BuildQueryString([

                client_id = getClientIdFromEnv(env),

                response_type = "code",

                state = state, // added by VM

                redirect_uri = redirect_uri])

        in

            [

                LoginUri = AuthorizeUrl,

                CallbackUri = redirect_uri,

                WindowHeight = windowHeight,

                WindowWidth = windowWidth,

                Context = env

            ],

 I need another parameter as input from the user now. It's called hostname in ui. How do I pass hostname and environment both to StartLogin function? I basically need these two variables to construct resourceUrlAny references would be helpful too.

1 Answer

0 votes
by (47.2k points)

we don't need to pass the variables into StartLogin function to construct the AuthorizeUrl.Instead of that, we can declare those variables as global variables so that the StartLogin can access the variables to construct the AuthorizeUrl.

Example:

hostname = ...; 

environment = ...;

authorize_uri = hostname & "/" & getOAuthUrlFromEnvName(environment) & "/oauth/authorize?"

StartLogin = (resourceUrl, state, display) =>

    let

        authorizeUrl = authorize_uri & "?" & Uri.BuildQueryString([

        …

Browse Categories

...