Forum Discussion

ktroyer's avatar
ktroyer
Regular Visitor
4 years ago

Creating a custom connection to Power BI using M via Visual Studios

Hello, 

I am attempting to create my first custom connection from an external electronic health program (using OAuth2) to power bi for visualization. The authorization process is complicated where I have to request and exchange a token each time it is accessed. 

 

My end goal is to login to power bi, choose the custom connector and create a dashboard. 

I have been unable to get my code to work. Visual Studios lists 0 errors, but when I run it I get the message " 0 arguments were passed to a function which expects 1". 

 

I would like help clearing the error and any advice anyone can provide is appreciated. (full code is included below)

section Chrono;

Profile="AAAA";
Client_ID="XXXX";
Client_Secret="YYY";
redirect_uri="https://oauth.powerbi.com/views/oauthredirect.html";
token_uri="https://drchrono.com/o/token";
authorize_uri="https://drchrono.com/o/authorize";
logout_uri="https://loginmicrosoftonline.com/logout.srf";
scope="patients:summary:read";
error_description="Error authorizing application";


//Login modal window dimensions

windowWidth=720;
windowHeight=1024;

[DataSource.Kind="Chrono", Publish="Chrono.Publish"]
shared Chrono.Contents=(url as text) =>
    let
        source=Json.Document(Web.Contents("https://drchrono.com"))

    in
        source;

//Data source kind description

Chrono=[
    TestConnection=(dataSourcePath)=>{"Chrono.Contents",dataSourcePath},
    Authentication=[
        OAuth=[
         StartLogin=StartLogin,
         FinishLogin=FinishLogin,
         Refresh=Refresh,
         Logout=Logout
        ]
    ],
    Label=Extension.LoadString("DataSourceLabel")
];

//Helper functions for OAuth2

StartLogin=(resourceUrl,state,display)=>
    let
        authorizeUrl=authorize_uri & "?" & Uri.BuildQueryString([
            response_type="code",
            client_id=Client_ID,
            redirect_uri=redirect_uri,
            state=state, 
            scope="patients:summary:read"
        ])
    in 
        [
            LoginUri=authorizeUrl,
            CallbackUri=redirect_uri,
            WindowHeight=720,
            WindowWidth=1024,
            Context=null 
        ];

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

Refresh=(resourceUrl,refresh_token)=>TokenMethod("refresh_token","refresh_token",refresh_token);
Logout=(token)=>logout_uri;

GetChronoData = (ChronoId as text)=>
    let
        //https://drchrono.com/o/authorize/?redirect_uri=REDIRECT_URI_ENCODED&response_type=code&client_id=CLIENT_ID_ENCODED&scope=SCOPES_ENCODED

        queryString=[
            client_id = Client_ID,
            scope = "patients:summary:read",
            format = "json",
            redirect_uri = "https://oauth.powerbi.com/views/oauthredirect.html",
            code="GET",
            response_type=":/api/patients_summary"
        ],
        source=Table.FromRows(Json.FromValue(Web.Contents("https://drchrono.com/o/authorize/?redirect_uri=REDIRECT_URI_ENCODED&response_type=code&client_id=CLIENT_ID_ENCODED&scope=SCOPES_ENCODED",[Query=queryString])))
    in
        source;
        

GetChronoId = (profile as text)=>
    let 
        source=Table.FromRows(Json.FromValue(Web.Contents("https://drchrono.com/o/authorize"))),
        first=Table.FirstValue(source)
    in
        first;

GetResponse = (client_id as text)=>
    let
        //https://drchrono.com/o/token/

        queryString=[
            refresh_token="get_refresh_token()",
            grant_type= "refresh_token", 
            client_id="XXXX",
            client_secret="YYYY"
        ],
        source=Table.FromRows(Json.FromValue(Web.Contents("https://drchrono.com/o/token/",[Query=queryString])))
    in
        source; 

// Token method from Dr.Chrono "https://drchrono.com/o/token"

TokenMethod=(grantType,tokenField,code)=>
    let
        queryString=[
            code="refresh_token",
            grant_type="refresh_token",
            redirect_uri="https://oauth.powerbi.com/views/oauthredirect.html",
            client_id="XXXXX",
            client_secret="YYY",
            access_token="access_token",
            refresh_token="refresh_token"
        ],
        tokenResponse=Web.Contents("https://drchrono.com/o/token",[Query=queryString])
    in 
        tokenResponse;


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

Chrono.Icons = [
    Icon16 = { Extension.Contents("Chrono16.png"), Extension.Contents("Chrono20.png"), Extension.Contents("Chrono24.png"), Extension.Contents("Chrono32.png") },
    Icon32 = { Extension.Contents("Chrono32.png"), Extension.Contents("Chrono40.png"), Extension.Contents("Chrono48.png"), Extension.Contents("Chrono64.png") }
];

Thank you, 

 

KT

 

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi ktroyer ,

     

    According to this error message : " 0 arguments were passed to a function which expects 1", it means you used a function that should have one parameter, but you did not put any values in it.

     

    For example,

    I want to use Text.Length() to get the length of the column, but I did not put the column name, there will be such an error:

     

    So please click the error / "Go to error" / check the error Details to find the error function.

     

     

    Best Regards,
    Eyelyn Qin

  • ktroyer's avatar
    ktroyer
    Regular Visitor

    Thank you for responding Eyelyn, but the connector is not created due to this error, so it cannot be opened in power BI. In the Visual Studio the Error list says " No issues found", but when I run the connection I get the error 0 arguments passed to a function which expects 1. 

  • Anonymous's avatar
    Anonymous
    Not applicable

    ktroyerI'm trying to build a custom connector too, in my case, for Google BigQuery, but the Power Query SDK does not work on Visual Studio anymore. How are you building?