Forum Discussion

zzinoun's avatar
zzinoun
Resolver I
3 years ago

Custom Connector Issue (mez file)

Hi ,

 

I am trying to use a custom connector to set up an automatoc refresh. However, I am stuck at this issue :

 

I guess this comes from a wrong parameter in the connector code but I could not determine the wrong part.

 

Here is the connector I am using (mez file).

 

section ProjectInsights;

[DataSource.Kind="ProjectInsights", Publish="ProjectInsights.Publish"]
shared ProjectInsights.Contents = (ProjectId as text) =>
let
    source = OData.Feed(
        Extension.LoadString("ProjectInsightsFeed") & ProjectId,
        null,
        [ ODataVersion = 4, MoreColumns = true ]
    )
in
    source;
openidConfiguration = Json.Document(Web.Contents(Extension.LoadString("WellKnown")));
ProjectInsights = [
    Authentication = [
        OAuth = [
            StartLogin = StartLogin,
            FinishLogin = FinishLogin,
            Label = "Bentley Account"
        ]
    ],
    Label = Extension.LoadString("DataSourceLabel")
];
client_id = Extension.LoadString("ClientId");
windowWidth = 600;
windowHeight = 600;
code_challenge = Binary.ToText(Text.ToBinary(Text.NewGuid()), BinaryEncoding.Base64);
StartLogin = (resourceUrl, state, display) =>
let
    qs = Uri.BuildQueryString([
        client_id = client_id,
        state = state,
        response_type = "code",
        scope=Extension.LoadString("Scope"),
        code_challenge = code_challenge,
        redirect_uri = redirect_uri
    ]),
    AuthorizeUrl = openidConfiguration[authorization_endpoint] & "?" & qs
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("authorization_code", parts[code])
in
    result;
TokenMethod = (grantType, code) =>
let
    query = [
        client_id = client_id,
        code = code,
        grant_type = "authorization_code",
        code_verifier = code_challenge,
        redirect_uri = redirect_uri
    ],
    queryWithCode = if (grantType = "refresh_token") then [ refresh_token = code ] else [code = code],
    Response = Web.Contents(
        openidConfiguration[token_endpoint],
        [
            Content = Text.ToBinary(Uri.BuildQueryString(query & queryWithCode)),
            Headers = [
                #"Content-type" = "application/x-www-form-urlencoded",
                #"Accept" = "application/json"
            ],
            ManualStatusHandling = { 400 }
        ]
    ),
    Parts = Json.Document(Response),
    Result =
        if (Record.HasFields(Parts, {"error", "error_description"}))
            then
                error Error.Record(Parts[error], Parts[error_description], Parts)
            else
                Parts
in
    Result;
Refresh = (resourceUrl, refresh_token) => TokenMethod("refresh_token", refresh_token);
 
 
Could you please advise  ?
 
Thank you,
No RepliesBe the first to reply