Showing posts with label Postman. Show all posts
Showing posts with label Postman. Show all posts

Wednesday, January 1, 2025

How to assign the generated token to a global variable and use it in Postman

How to assign the generated token to a global variable and use of environments in Postman:

            Copying the token and assigning it to different requests can be difficult. Instead, you can create a variable and assign the token to that variable. In your requests, you can use the variable directly. Simply trigger the token URL, and then proceed to trigger your GET or POST requests.

Generate Token and Assign to Global variable: 

           1. Click on the main collection, which will populate the right-side window. Select the "Variables" tab and add the variables you need.

                For the Resource variable, I am assigning my dev machine URL.

            2. For the AccessToken variable field, I want to capture the generated token after triggering the API.

            3.Create an environment using the option at the top right side.

            4.After creating the environment, select the highlighted option and click on "Edit." Add the variable and save it.

            5. To capture the token, write the following JavaScript code under the "Scripts" tab of the API request that generates the token:

    var access_token = pm.response.json().access_token;
    pm.environment.set("accessToken", access_token)

            6. After clicking the "Send" button, the system will assign the generated token to my "AccessToken" variable.


Different Environments:

            Instead of creating different variables, we can create different environments and maintain different values for the same variable based on the environment.

1.On the top-right corner, you will find the Environment window. Click on the + symbol to create a new environment.

2. Alternatively, you can click on the Environments option on the left side. Here, you will see the environments available in Postman.

3. You can create variables as required for your process.

4. Maintain the same variable name across all environments.

5. Use this variable in your request and click Send. For testing purposes, I have provided a dummy name.

Keep Daxing!!

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!!

Generate token from key vault parameters using x++ in D365FO.

 Generate token from key vault parameters using x++ in D365FO.

  •  We are saving our token URL in Key vault for security. By using the below code we will get the URL form Key Vault. 
  • We have used same client ID and client secret for key vault also. So I have configured that in key vault parameters. 

    
    str                           url, token;
    KeyVaultParameters            vaultParameter;
    KeyVaultCertificateTable      vaultCertificate;

    select vaultParameter where vaultParameter.Name == 'Name';// which we have created in Key vault.

    if (vaultParameter)
    {
        select vaultCertificate where vaultCertificate.KeyVaultRef == vaultParameter.RecId;

        KeyVaultClientHelper::removeCachedKeyVaultClient(vaultCertificate.KeyVaultCertificate);

        url = KeyVaultCertificateHelper::getManualSecretValue(vaultCertificate.RecId);
        // Here KeyVault will generate the Token URL.
AuthenticationResult authResult; ClientCredential clientCrendential = new ClientCredential(                     vaultParameter.AppClientId, vaultParameter.getClientSecretKey());                     // Based on client Id and client secret key we have created Key vault. AuthenticationContext authContext = new AuthenticationContext(url); authResult = authContext.AcquireToken('Scope or Resource(URL)', clientCrendential);         token = authResult.AccessToken; Info(token); }



Keep Daxing!!

Tuesday, April 19, 2022

Postman Set up with D365FO

 Hi guys, Today we will see the steps to setup Postman with D365FO.


Step 1: Create a new application in Azure Platform to Register D365FO.

Step 2: After Registration we nee to copy the client Id and we need to add client id in D365FO.

URL/System administration/Setup/Azure Active Directory applications.

Step 3: Open the Postman and create a new collection. It has been renamed to 'Test API'.



Step 4: Click on Add request to get the token. Copy the 'TenantId' from the Azure portal.

https://login.microsoftonline.com/tenantId/oauth2/token


Step 5: Place the above link and select tab Body and click on 'x-www-form-urlencoded' or 'form-data' radio button.


Step 6: Provide below Key and Value details.

Key         Value

client_Id                   ClientID value 

grant_type client_credentials

client_secret Client secret

resource         D365FO Url

------------or--------------------

scope D365FO Url/.default

audience         D365FO Url


we can use resource or scope, audience.


Step 7: Click on Send button to get the access token.




To test this I am getting the customer groups from D365FO.

Step 8: Click on Add request to get the response.

Add this URL:D365FO url/data/CustomerGroups 

Step 9: Click on Authorization and select Bearer token and copy the access token. Click send button.



Keep Daxing!!