GET Token & Get Authorization By Oaths in Business Central

procedure GetAuthorization(): Boolean;
    begin
        EInvoiceAPISetup.Get();
        CheckSetup();
        ResetVar();
        ResponseText := '';
        JSObject.Add('user_account_no', EInvoiceAPISetup."User Account No");
        JSObject.Add('connectorid', EInvoiceAPISetup."Connector Id");
        JSObject.WriteTo(RequestText);
        MyHttpContent.WriteFrom(RequestText);
        MyHttpContent.GetHeaders(ContentHeaders);
        ContentHeaders.Clear();
        //ContentHeaders.Add('User-Agent', 'Dynamics 365 Business Central');
        ContentHeaders.Add('Content-Type', 'application/json; charset=utf-8');
        ContentHeaders.Add('Secret-Key', EInvoiceAPISetup."Secret-Key");
        MyHttpClient.DefaultRequestHeaders.Add('User-Agent', 'Dynamics 365 Business Central');
        Fullurl := EInvoiceAPISetup."API Base Url" + AuthenticateTxt;
        if MyHttpClient.Post(Fullurl, MyHttpContent, MyHttpResponseMessage) then begin
            MyHttpResponseMessage.Content().ReadAs(ResponseText);

            UpdateRecord(ResponseText, 'AUTHORIZATION');
            exit(True);
        end ELSE
            if not MyHttpResponseMessage.IsSuccessStatusCode() then
                exit(false);
        // Error('The web service returned an error message:\\' +
        //         'Status code: ' + Format(MyHttpResponseMessage.HttpStatusCode()) +
        //         'Description: ' + MyHttpResponseMessage.ReasonPhrase());


    end;




//Keep Simple
procedure GetTokenFinal()
    var
        HttpClient: HttpClient;
        HttpHeaders: HttpHeaders;
        HttpRequestMessage: HttpRequestMessage;
        HttpResponseMessage: HttpResponseMessage;
    begin
        HttpRequestMessage.Method := 'GET';
        HttpRequestMessage.SetRequestUri := '';
        HttpRequestMessage.GetHeaders(HttpHeaders);
        HttpHeaders.Add('Secret-Key', '');
        HttpHeaders.Add('Authorization', 'Bearer ');
        IF not HttpClient.Send(HttpRequestMessage, HttpResponseMessage) then
            IF HttpResponseMessage.IsBlockedByEnvironment then
                Error('Blocked By Environment')

            else
                Error('Internet Connection Failed');
        if not HttpResponseMessage.IsSuccessStatusCode() then
            Error('The web service returned an error message:\\' +
                    'Status code: ' + Format(HttpResponseMessage.HttpStatusCode()) +
                    'Description: ' + HttpResponseMessage.ReasonPhrase());
        HttpResponseMessage.Content().ReadAs(ResponseText);
        Message('%1', ResponseText);

    end;



procedure IsExpireAuthorization(): Boolean
    var
        RemainingTime: Duration;
    begin
        EInvoiceAPISetup.Get();
        if EInvoiceAPISetup.Expires <> 0DT then begin
            RemainingTime := (EInvoiceAPISetup."Expires In Hours" * 1000)
            - (System.CurrentDateTime() - EInvoiceAPISetup.Expires);
            if RemainingTime > 0 then
                exit(false);
        end;
        exit(true);

    end;

    procedure IsExpireToken(): Boolean
    var
        RemainingTime: Duration;
    begin
        EInvoiceAPISetup.Get();
        if EInvoiceAPISetup.Expires <> 0DT then begin
            RemainingTime := (EInvoiceAPISetup."Expires In Hours" * 1000)
         - (System.CurrentDateTime() - EInvoiceAPISetup.Expires);
            if RemainingTime > 0 then
                exit(false);
        end;
        exit(true);

    end;


No comments:

Post a Comment