procedure GetAccessToken() AccessToken: Text
var
OAuth2: Codeunit OAuth2;
PromptInteraction: Enum "Prompt Interaction";
Scopes: List of [Text];
AuthError: Text;
Share: Record "Custom Company Setup";
begin
Share.Reset();
Share.ChangeCompany("Company Name");
if Share."Sharepoint URL" <> '' then begin
Scopes.Add('https://graph.microsoft.com/.default');
Rec.Get(CompanyName);
OAuth2.AcquireAuthorizationCodeTokenFromCache(GraphMgtGeneralTools.StripBrackets(Format("Application ID")),
"Client Secret", "Redirect URL", "OAuth Redirect Url", Scopes, AccessToken);
if AccessToken <> '' then
exit;
OAuth2.AcquireTokenByAuthorizationCode(GraphMgtGeneralTools.StripBrackets(Format("Application ID")),
"Client Secret", "OAuth Redirect Url", "Redirect URL", Scopes, PromptInteraction::"Select Account",
AccessToken, AuthError);
if AccessToken = '' then
Error(AuthError);
end;
end;