Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
asusmani
New Member

Custom Connector - Test connection fails when setting up gateway

Hello all,

I have built a custom connector that uses OAuth authentication. I set it as per the documentation so i have a StartLogin help function and a FinishLogin along with a TestConnection handler. 

 

My connector is pulling data from an endpoint that requires a token exchange.

I have it all working just fine on the client when I use the connector. It does what I want it to.

 

However, once I publish the report and attempt to set up the gateway connection on the power bo portal, I get an issue when it tries to test the connection when we click the Edit Credentials button. For some reason, it seems to go to another URL from what I can see other than the authorization URL in the code.

 

 

I can run it successfully in visual studio when debugging and from my power bi client.  I just can't get past the gateway set up screen when it  tries to test the connection.... 

 

Any ideas?

 

 

 

 

 

 

// This file contains your Data Connector logic
section PowerBiConnectorTest;

// OuraCloud OAuth2 values
client_id = "<my client id>";
client_secret = "<my client secret>";
redirect_uri = "https://localhost:8080";
token_uri = "https://login.microsoftonline.com/common/oauth2/token";
authorize_uri = "https://login.microsoftonline.com/common/oauth2/authorize";
logout_uri = "https://login.microsoftonline.com/logout.srf";

// Login modal window dimensions
windowWidth = 720;
windowHeight = 1024;


[DataSource.Kind="PowerBiConnectorTest", Publish="PowerBiConnectorTest.Publish"]
shared PowerBiConnectorTest.Contents = (url as text) =>
    let

       apiKey = Extension.CurrentCredential()[access_token],
        headers = [
        #"x-api-key" = "<my api key>",
        #"Authorization" = "Bearer " & apiKey,
        #"Content-Type" = "application/json"
        ],
        source = Web.Contents(url, [ Headers = headers, ManualCredentials = true ])

    in
        source; 

// Data Source Kind description
PowerBiConnectorTest = [
    TestConnection = (dataSourcePath) => { "PowerBiConnectorTest.Contents", dataSourcePath },
    Authentication = [
        OAuth = [
            StartLogin=StartLogin,
            FinishLogin=FinishLogin
        ]
    ],
    Label = Extension.LoadString("DataSourceLabel")
];

// Data Source UI publishing description
PowerBiConnectorTest.Publish = [
    Beta = true,
    Category = "Other",
    ButtonText = { Extension.LoadString("ButtonTitle"), Extension.LoadString("ButtonHelp") },
    LearnMoreUrl = "https://powerbi.microsoft.com/",
    SourceImage = PowerBiConnectorTest.Icons,
    SourceTypeImage = PowerBiConnectorTest.Icons
];

// Helper functions for OAuth2: StartLogin, FinishLogin, Refresh, Logout
StartLogin = (resourceUrl, state, display) =>
    let
        authorizeUrl = authorize_uri & "?" & Uri.BuildQueryString([
            response_type = "code",
            client_id = client_id,  
            response_mode = "query",
            redirect_uri = redirect_uri
        ])
    in
        [
            LoginUri = authorizeUrl,
            CallbackUri = redirect_uri,
            WindowHeight = 720,
            WindowWidth = 1024,
            Context = null
        ];

FinishLogin = (context, callbackUri, state) =>
    let
        // parse the full callbackUri, and extract the Query string
        parts = Uri.Parts(callbackUri)[Query],
        // if the query string contains an "error" field, raise an error
        // otherwise call TokenMethod to exchange our code for an access_token
        result = if (Record.HasFields(parts, {"error", "error_description"})) then 
                    error Error.Record(parts[error], parts[error_description], parts)
                 else
                    TokenMethod("authorization_code", "code", parts[code])
    in
        result;

//Refresh = (resourceUrl, refresh_token) => TokenMethod("refresh_token", "refresh_token", refresh_token);
//Refresh = (resourceUrl, refresh_token) => TokenMethod("authorization_code", "code", refresh_token);


//Logout = (token) => logout_uri;

// see "Exchange code for access token: POST /oauth/token" at https://cloud.ouraring.com/docs/authentication for details
TokenMethod = (grantType, tokenField, code) =>
    let
        queryString = [
            grant_type = "authorization_code",
            redirect_uri = redirect_uri,
            client_id = client_id,
            client_secret = client_secret
        ],
        queryWithCode = Record.AddField(queryString, tokenField, code),

        tokenResponse = Web.Contents(token_uri, [
            Content = Text.ToBinary(Uri.BuildQueryString(queryWithCode)),
            Headers = [
                #"Content-type" = "application/x-www-form-urlencoded"
            ]
        ]),
        body = Json.Document(tokenResponse),
        result = if (Record.HasFields(body, {"error", "error_description"})) then 
                    error Error.Record(body[error], body[error_description], body)
                 else
                    body
    in
        result;

Value.IfNull = (a, b) => if a <> null then a else b;



PowerBiConnectorTest.Icons = [
    Icon16 = { Extension.Contents("PowerBiConnectorTest16.png"), Extension.Contents("PowerBiConnectorTest20.png"), Extension.Contents("PowerBiConnectorTest24.png"), Extension.Contents("PowerBiConnectorTest32.png") },
    Icon32 = { Extension.Contents("PowerBiConnectorTest32.png"), Extension.Contents("PowerBiConnectorTest40.png"), Extension.Contents("PowerBiConnectorTest48.png"), Extension.Contents("PowerBiConnectorTest64.png") }
];

 

 

 

 

 

 

3 REPLIES 3
asusmani
New Member

Here is a screenshot of what it is doing:

2021-09-30_11-30-14.png

Hi @asusmani 

OAuth for custom connectors via gateways is currently supported only for gateway admins but not other data source users. Please check whether you are the gateway admin.

For reference: Handling Gateway Support

 

Please check your network, and add the url in white list.

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hello @v-rzhou-msft,

 

We were actually trying to hit that test link with gateway admin access. 

Some questions:


1. Regarding the url to whitelist, which URL do I need to add to the whitelist? My endpoint I am pulling data from? or the one in the screenshot?

 

2. Is just whitelisting from my network/firewall? Or does Power BI have some sort of whitelist control?

 

Thanks.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.