Wednesday, July 13, 2022

Generate Bearer token using x++ in D365FO.

  Generate Bearer token using x++ in D365FO.

  • While triggering API we need to generate a token.
  • While calling their API We need to send the token, So they will validate that Token and send the response data If i.e. valid.
  • To Generate Token we need
    • Client Id
    • Client secret
    • Tenant Id
  • For the above details, we need to do the app registration in the Azure portal.
  • We need to create one record with the client id in the Azure application in the system administration module.

    AuthenticationResult        authResult; 

        
    ClientCredential        clientCrendential   = new ClientCredential('ClientId', 'ClientSecretKey');
    AuthenticationContext   authContext         = new AuthenticationContext(url);
            // https://login.microsoftonline.com/tenantId/oauth2/token
        
    authResult  = authContext.AcquireToken('Scope or Resource(URL)', clientCrendential);
        
    token =  authResult.AccessToken;

    Info(token);


Scope: ABCD/.default

--> api//:ABCD/.default 

    --> We need to remove api//: 

    --> Otherwise it will throw the error.


Keep Daxing!!

No comments:

Post a Comment