Forum Discussion

kmouneca's avatar
kmouneca
Frequent Visitor
3 years ago

Refresh fails for Custom Api datasource

Hi Team,
Issue : Refresh in power bi service is not working for my dashboard which uses api as datasource.
Reference : https://alexdmeyer.com/2020/06/16/how-to-configure-power-bi-to-utilize-a-custom-api-as-a-data-source/

the above-mentioned article  suggested how to configure power bi to utilize custom api as datasource

with the help of that I successfully created the dataset.

 

I am connecting it anonymously and gave privacy level as organizational.

when i do my refresh from powerbi desktop it works as expected.


In power bi service the refresh failing, with the following error



 



In Data Source Credentials, i gave "skip test Connection" after which also refresh fails.

In manage connection and gateway setting, my api connection is Cloud based one. Therefore its not asking for gateway connection.

 

 

could anyone please help here,  to make my refresh work in power bi service ?

3 Replies

  • kmouneca's avatar
    kmouneca
    Frequent Visitor

    Hi v-zhangti ,

    Thank you for your time and help. I followed the first link and created custom connector when i try to load that into power bi. It pops up with the following error.

     

    Am i missing something here ?

     

    // This file contains your Data Connector logic
    section Testingproject;
    
    // TODO: add your client id and secret to the embedded files
    
    client_id = "xxx";
    client_secret = "xxx";
    
    redirect_uri = "https://oauth.powerbi.com/views/oauthredirect.html";
    windowWidth = 800;
    windowHeight = 800;
    logout_uri = "https://login.microsoftonline.com/logout.srf";
    BaseUrl = "https://xxxxxxxx/reporting-xxxxx/stats";
    OAuthBaseUrl = "https://xxxx.okta.com/oauth2/xxxxx/v1/token";
    
    // OAuth2 scope
    scopes = {
        "/service/xxxxxx.Dashboard.read"
    };
    
    [DataSource.Kind="Testingproject", Publish="Testingproject.Publish"]
    shared Testingproject.Contents = (url as text) =>
        let
            token = Extension.CurrentCredential()[access_token],
          
           headers = [Authorization = "Bearer " & token],
           Report = Json.Document(Web.Contents(url, [Headers = [#"Content-Type" = "application/json", Authorization=headers]]))
        in
           
            Report;
    
    // Data Source Kind description
    Testingproject = [
        TestConnection = (dataSourcePath) => { "Testingproject.Contents", dataSourcePath },
        Authentication = [
            OAuth = [
                StartLogin = StartLogin,
                FinishLogin = FinishLogin,
                Refresh = Refresh,
                Logout=Logout
            ]
        ],
        Label = Extension.LoadString("DataSourceLabel")
    ];
    
    // Data Source UI publishing description
    Testingproject.Publish = [
        Beta = true,
        Category = "Other",
        ButtonText = { Extension.LoadString("ButtonTitle"), Extension.LoadString("ButtonHelp") },
        LearnMoreUrl = "https://powerbi.microsoft.com/",
        SourceImage = Testingproject.Icons,
        SourceTypeImage = Testingproject.Icons
    ];
    
    StartLogin = (resourceUrl, state, display) =>
        let
            AuthorizeUrl = OAuthBaseUrl
                & "?"
                & Uri.BuildQueryString(
                    [
                        client_id = client_id,
                        response_type = "code",
                        state = state,
                        redirect_uri = redirect_uri,
                        scope = scopes
                    ]
                )
        in
            [
                LoginUri = AuthorizeUrl,
                CallbackUri = redirect_uri,
                WindowHeight = windowHeight,
                WindowWidth = windowWidth,
                Context = null
            ];
    
    
    FinishLogin = (context, callbackUri, state) =>
        let
            parts = Uri.Parts(callbackUri)[Query],
    
            result = if (Record.HasFields(parts, {"error", "error_description"})) then 
                        error Error.Record(parts[error], parts[error_description], parts)
                     else
                        TokenMethod("code", parts[code])
        in
            result;
    
    Refresh = (resourceUrl, refresh_token) => TokenMethod("refresh_token", "refresh_token", refresh_token);
    
    Logout = (token) => logout_uri;
    
    TokenMethod = (tokenField, code) =>
        let
            tokenResponse = Web.Contents(
                OAuthBaseUrl,
                [
                    Content = Text.ToBinary(
                        Uri.BuildQueryString(
                            [
                                client_id = client_id,
                                client_secret = client_secret,
                                code = code,
                                scope = scopes,
                                grant_type = "client_credentials",
                                redirect_uri = redirect_uri
                            ]
                        )
                    ),
                    Headers = [#"Content-type" = "application/x-www-form-urlencoded", #"Accept" = "application/json"]
                ]
            ),
            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;
    
    Testingproject.Icons = [
        Icon16 = { Extension.Contents("Testingproject16.png"), Extension.Contents("Testingproject20.png"), Extension.Contents("Testingproject24.png"), Extension.Contents("Testingproject32.png") },
        Icon32 = { Extension.Contents("Testingproject32.png"), Extension.Contents("Testingproject40.png"), Extension.Contents("Testingproject48.png"), Extension.Contents("Testingproject64.png") }
    ];

     


    I am new to this mquery functionality. Could you please take a look and let me know what i am missing over here?