<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic &amp;quot;Object Reference not set to instance&amp;quot; error in Custom Connector Power BI OAuth FinishLogin in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/quot-Object-Reference-not-set-to-instance-quot-error-in-Custom/m-p/3362516#M43710</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[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.&amp;lt;&amp;gt;c__DisplayClass11_0.&amp;lt;SetCredentalInteractive&amp;gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;// 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) =&amp;gt;
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) =&amp;gt; {"Dev.Contents",dataSourcePath},
    Authentication = [
        OAuth = [
            StartLogin=StartLogin,
            FinishLogin=FinishLogin
        ]
    ],
    Label = "Login"
];

StartLogin = (resourceUrl, state, display) =&amp;gt;
    let
        AuthorizeUrl = "https://authAPI/OAuth/Authorize?" &amp;amp; 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) =&amp;gt; 
    let 
        Parts = Uri.Parts(callbackUri)[Query]
    in 
        TokenMethod(Parts[code]);


TokenMethod = (code) =&amp;gt;
    let
        Response = Web.Contents(
            "https://tokenAPI/OAuth/api/oauth/token?" &amp;amp; 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) =&amp;gt;
    let 
        Response = Web.Contents(
            "https://loginAPI/OAuth/api/login/oauth",
            [
                Content = Text.ToBinary("{""token"":""" &amp;amp; tempToken &amp;amp; """}"),
                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") }
];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Aug 2023 21:52:52 GMT</pubDate>
    <dc:creator>clongaw</dc:creator>
    <dc:date>2023-08-02T21:52:52Z</dc:date>
    <item>
      <title>"Object Reference not set to instance" error in Custom Connector Power BI OAuth FinishLogin</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/quot-Object-Reference-not-set-to-instance-quot-error-in-Custom/m-p/3362516#M43710</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[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.&amp;lt;&amp;gt;c__DisplayClass11_0.&amp;lt;SetCredentalInteractive&amp;gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;// 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) =&amp;gt;
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) =&amp;gt; {"Dev.Contents",dataSourcePath},
    Authentication = [
        OAuth = [
            StartLogin=StartLogin,
            FinishLogin=FinishLogin
        ]
    ],
    Label = "Login"
];

StartLogin = (resourceUrl, state, display) =&amp;gt;
    let
        AuthorizeUrl = "https://authAPI/OAuth/Authorize?" &amp;amp; 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) =&amp;gt; 
    let 
        Parts = Uri.Parts(callbackUri)[Query]
    in 
        TokenMethod(Parts[code]);


TokenMethod = (code) =&amp;gt;
    let
        Response = Web.Contents(
            "https://tokenAPI/OAuth/api/oauth/token?" &amp;amp; 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) =&amp;gt;
    let 
        Response = Web.Contents(
            "https://loginAPI/OAuth/api/login/oauth",
            [
                Content = Text.ToBinary("{""token"":""" &amp;amp; tempToken &amp;amp; """}"),
                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") }
];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2023 21:52:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/quot-Object-Reference-not-set-to-instance-quot-error-in-Custom/m-p/3362516#M43710</guid>
      <dc:creator>clongaw</dc:creator>
      <dc:date>2023-08-02T21:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: "Object Reference not set to instance" error in Custom Connector Power BI OAuth Finish</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/quot-Object-Reference-not-set-to-instance-quot-error-in-Custom/m-p/3364276#M43727</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;StartLogin = (resourceUrl, state, display) =&amp;gt;
    let
        authorizeUrl = "https://authorizeUrl/OAuth/Authorize?client_id=PowerBI&amp;amp;state=" &amp;amp; state &amp;amp; "&amp;amp;redirect_uri=https://oauth.powerbi.com/views/oauthredirect.html"
    in
        [
            LoginUri = "https://authorizeUrl/OAuth/Authorize?client_id=PowerBI&amp;amp;state=" &amp;amp; state &amp;amp; "&amp;amp;redirect_uri=https://oauth.powerbi.com/views/oauthredirect.html",
            CallbackUri = "https://oauth.powerbi.com/views/oauthredirect.html",
            WindowHeight = 800,
            WindowWidth = 700,
            Context = null
        ];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 19:55:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/quot-Object-Reference-not-set-to-instance-quot-error-in-Custom/m-p/3364276#M43727</guid>
      <dc:creator>clongaw</dc:creator>
      <dc:date>2023-08-03T19:55:27Z</dc:date>
    </item>
  </channel>
</rss>

