10

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 powerbi.

    // 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 resourceUrl. Any references would be helpful too.

Kimchy
  • 501
  • 8
  • 24
sudeepdino008
  • 3,194
  • 5
  • 39
  • 73

1 Answers1

3

You don't need to pass the variables into StartLogin function to construct the AuthorizeUrl. Instead, you can just declare them as global variables so the StartLogin can access them to construct the AuthorizeUrl.

e.g.

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

StartLogin = (resourceUrl, state, display) =>
    let
        authorizeUrl = authorize_uri & "?" & Uri.BuildQueryString([
        ...
Foxan Ng
  • 6,883
  • 4
  • 34
  • 41
  • 1
    and how do I assign the hostname and environment? How will the values entered by user end up in them? – sudeepdino008 Aug 29 '17 at 05:56
  • You can refer to this [example](https://github.com/Microsoft/DataConnectors/tree/master/samples/HelloWorldWithDocs) for how to obtain user input. – Foxan Ng Aug 29 '17 at 08:05
  • @FoxanNg your example does not seem helpful. You claim these global variables can be set by user input but the example does not show this. Can this even be done? Power query variables are immutable. – Kurren Jul 04 '22 at 12:46