case APIHeader."Auth Type" of
APIHeader."Auth Type"::"OAuth 1.0", APIHeader."Auth Type"::"OAuth 2.0":
begin
RequestAccessToken(APIHeader, ErrorContent, AccessToken);
Client.DefaultRequestHeaders().Add(
'Authorization',
'Bearer ' + AccessToken);
end;
APIHeader."Auth Type"::"Bearer Token":
begin
Client.DefaultRequestHeaders().Add(
'Authorization',
'Bearer ' + APIHeader.GetClientSecret());
end;
APIHeader."Auth Type"::Basic:
begin
Client.DefaultRequestHeaders().Add(
'Authorization',
'Basic ' + Base64Convert.ToBase64(StrSubstNo('%1:%2', APIHeader.Login, APIHeader.GetClientSecret())));
end;
end;
procedure RequestAccessToken(var APIHeader: Record "AOG API Header"; var ErrorContent: Text; var AccessToken: Text) IsConnected: Boolean
var
OAuth2: Codeunit OAuth2;
ListOfScopes: List of [Text];
begin
ListOfScopes.Add(APIHeader.Scope);
case APIHeader."Grant Type" of
APIHeader."Grant Type"::"Authorization Code":
IsConnected := OAuth2.AcquireTokenByAuthorizationCode(APIHeader."Client ID", APIHeader.GetClientSecret(),
APIHeader."Authorization URL", APIHeader."Redirect URL", ListOfScopes, Enum::"Prompt Interaction"::None,
AccessToken, ErrorContent);
APIHeader."Grant Type"::"Client Credentials":
case APIHeader."Auth Type" of
APIHeader."Auth Type"::"OAuth 1.0":
begin
IsConnected := OAuth2.AcquireTokenWithClientCredentials(APIHeader."Client ID", APIHeader.GetClientSecret(),
APIHeader."Authorization URL", APIHeader."Redirect URL",
AccessToken, ErrorContent)
end;
APIHeader."Auth Type"::"OAuth 2.0":
begin
IsConnected := OAuth2.AcquireTokenWithClientCredentials(APIHeader."Client ID", APIHeader.GetClientSecret(),
APIHeader."Authorization URL", APIHeader."Redirect URL", ListOfScopes, AccessToken);
end;
end;
end;
if not IsConnected then
Error(GetLastErrorText());
end;