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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
clongaw
Frequent Visitor

"Object Reference not set to instance" error in Custom Connector Power BI OAuth FinishLogin

I'm trying to build a Custom Connector in Power BI and am running into an issue in the beginning of the OAuth flow. I'm able to successfully open the window for the user to log in, and successfully finish granting Power BI permission, but the Custom Connector immediately crashes and presents me with the following error upon the moment it attempts to retrieve the redirect_uri.

 

[Error]	[Task exited abnormally] pqtest set-credential pid(29464) exit(3762504530) stderr: 
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Mashup.Engine1.Runtime.Extensibility.ExtensionOAuthFactory.Provider.Microsoft.Mashup.OAuth.IOAuthProvider.FinishLogin(Byte[] serializedContext, Uri callbackUri, String state)
   at PQTest.Common.Commands.<>c__DisplayClass11_0.<SetCredentalInteractive>b__0()
   at PQTest.Common.Commands.ConfigureMashupEnvironmentAndRun(Configuration configuration, Action action)
   at PQTest.Console.Program.RunCredentialCommand(CommandLineApplication app, CommandLineApplication command, Action`1 invoke, DataSourceSetting dataSourceSetting)
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at PQTest.Console.Program.Main(String[] args)

 

My code is below and, despite the error, I believe the true issue lies in the StartLogin function or some background handling or parsing of the redirect_uri (which I've ensured is correct and correctly formatted). I've even gone so far as to remove the FinishLogin declaration, but still recieved the same error message. The TokenMethod and AccessToken have also been removed in testing to ensure that the error persists immediately after the redirect_uri is retrieved. 

 

// This file contains your Data Connector logic
[Version = "1.2.0"]
section Dev;

// OAuth Credentials
#"MyCompany" = "Company";
client_id = "client_id";
client_secret = "client_secret";
redirect_uri = "https://oauth.powerbi.com/views/oauthredirect.html";
windowWidth = 1200;
windowHeight = 1000;

[DataSource.Kind="Dev", Publish="Dev.Publish"]
shared Dev.Contents = Value.ReplaceType(GetDataImpl,GetDataType);

GetDataImpl = (url as text) =>
let 
      Data = Xml.Tables(Web.Contents(url, 
        [Headers = [#"Accept" = "*/*", #"Company" = #"MyCompany"]]))
  in
      Data;

GetDataType = type function (url as Uri.Type) as any;

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

StartLogin = (resourceUrl, state, display) =>
    let
        AuthorizeUrl = "https://authAPI/OAuth/Authorize?" & Uri.BuildQueryString([
            client_id = client_id,
            state = state,
            redirect_uri = redirect_uri])
    in
        [
            LoginUri = AuthorizeUrl,
            CallbackUri = redirect_uri,
            WindowHeight = windowHeight,
            WindowWidth = windowWidth,
            Context = ""
        ];

FinishLogin = (context, callbackUri, state) => 
    let 
        Parts = Uri.Parts(callbackUri)[Query]
    in 
        TokenMethod(Parts[code]);


TokenMethod = (code) =>
    let
        Response = Web.Contents(
            "https://tokenAPI/OAuth/api/oauth/token?" & Uri.BuildQueryString(
                [
                    client_id = client_id,
                    client_secret = client_secret,
                    code = code,
                    redirect_uri = redirect_uri
                ]
            ),
            [
                Headers = [#"Content-type" = "application/x-www-form-urlencoded", #"Accept" = "application/json", #"Company" = #"MyCompany"]
            ]
        ),
        Parts = AccessToken(Binary.ToText(Response))
    in
        Parts;

AccessToken = (tempToken) =>
    let 
        Response = Web.Contents(
            "https://loginAPI/OAuth/api/login/oauth",
            [
                Content = Text.ToBinary("{""token"":""" & tempToken & """}"),
                Headers = [#"Content-type" = "application/x-www-form-urlencoded", #"Accept" = "application/json", #"Company" = #"MyCompany"]
            ]
        ),
        Parts = Json.Document(Response)
    in 
        Parts;


// Data Source UI publishing description
Dev.Publish = [
    Beta = true,
    Category = "Other",
    ButtonText = { "Dev", "Dev" },
    LearnMoreUrl = "https://powerbi.microsoft.com/",
    SourceImage = Dev.Icons,
    SourceTypeImage = Dev.Icons
];

Dev.Icons = [
    Icon16 = { Extension.Contents("Dev16.png"), Extension.Contents("Dev20.png"), Extension.Contents("Dev24.png"), Extension.Contents("Dev32.png") },
    Icon32 = { Extension.Contents("Dev32.png"), Extension.Contents("Dev40.png"), Extension.Contents("Dev48.png"), Extension.Contents("Dev64.png") }
];

 

 

1 ACCEPTED SOLUTION
clongaw
Frequent Visitor

There were a few issues in the code regarding the API, but the largest one that was causing the unknown error was that the LoginUri field in StartLogin wasn't being populated correctly for some reason. By switching to explicitly stating the LoginUri, the OAuth flow is allowed to continue. 

StartLogin = (resourceUrl, state, display) =>
    let
        authorizeUrl = "https://authorizeUrl/OAuth/Authorize?client_id=PowerBI&state=" & state & "&redirect_uri=https://oauth.powerbi.com/views/oauthredirect.html"
    in
        [
            LoginUri = "https://authorizeUrl/OAuth/Authorize?client_id=PowerBI&state=" & state & "&redirect_uri=https://oauth.powerbi.com/views/oauthredirect.html",
            CallbackUri = "https://oauth.powerbi.com/views/oauthredirect.html",
            WindowHeight = 800,
            WindowWidth = 700,
            Context = null
        ];

 

View solution in original post

1 REPLY 1
clongaw
Frequent Visitor

There were a few issues in the code regarding the API, but the largest one that was causing the unknown error was that the LoginUri field in StartLogin wasn't being populated correctly for some reason. By switching to explicitly stating the LoginUri, the OAuth flow is allowed to continue. 

StartLogin = (resourceUrl, state, display) =>
    let
        authorizeUrl = "https://authorizeUrl/OAuth/Authorize?client_id=PowerBI&state=" & state & "&redirect_uri=https://oauth.powerbi.com/views/oauthredirect.html"
    in
        [
            LoginUri = "https://authorizeUrl/OAuth/Authorize?client_id=PowerBI&state=" & state & "&redirect_uri=https://oauth.powerbi.com/views/oauthredirect.html",
            CallbackUri = "https://oauth.powerbi.com/views/oauthredirect.html",
            WindowHeight = 800,
            WindowWidth = 700,
            Context = null
        ];

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.