共用方式為


How to call CRM WebAPI in Logic Apps

You probably often use web API provide by Azure/Microsoft in your web applications for customized requirements. Normally,
you should follow OAuth 2.0 code authorization flow to acquire the access token for the API in your web application. But
in Logic Apps, the story is different because all the actions of a work flow are executed in the back-end. In this blog,
we will discuss how to consume Azure API in Logic Apps. In our experiment, we will use CRM WebAPI as an example.

Part 1: Register an application in Azure AD.

    1. Log on Azure Classic Portal https://manage.windowsazure.com/
    2. Open the Active Directory.
      image001
    3. Click on Add button.
      image002
    4. Select "Add an application my organization is developing" on the popup dialog.
    5. Set the new application properties to:
      • Name: CRM OAuth2 Demo (you can use what name you want)
      • Type: Web application and/or web API
    1. Click Next button.
      image003
    2. Set the following application properties: (Any urls we can set here I think because we can change them later, just
      make sure the validations can be passed.)
    1. The new application titled CRM OAuth2 Demo will be added to the list of applications in the selected Azure
      Active Directory.
    2. Select the new application titled CRM OAuth2 Demo.
      image005
    3. The Application Configuration Tab is displayed with the Client Id.
      image006
    4. Configure a client secret for the application.
      To generate an access token, we need to configure a client secret.
      To do this we add a key to the application in Azure AD. Below is a step by step guide to do this.
      • Following on from the previous section, scroll down to the keys on the applications configuration screen
      • Create a new key by selecting 2 years in the Select Duration drop down list.
      • Click Save.
        image007
    1. Store the generated key (Client Secret) in a safe place because it will not be visible the next time the keys screen
      is displayed.
      image008
    1. Assign Dynamics CRM permissions to the Demo application
      • Continuing from the previous section, on the applications configuration scroll down to the permissions to other
        applications section.
      • Click on the Add application button.
        image009
      • The permissions to other applications dialog is displayed.
      • Select Dynamics CRM Online.
      • Click the Add button.
      • Click the Complete button (tick in the lower right corner of the dialog).
        image010
      • Dynamics CRM Online is added to the permissions to other applications list.
      • Check the Access CRM Online as organization user permission from the Delegated Permissions drop down list.
      • Click save.
        image011

Part 2: Acquire an access token and use it to call CRM WebAPI

    1. Add an action to acquire access token.
      image012
      Note*: Set Headers: Cache-Control: no-cache Content-Type:
      application/x-www-form-urlencoded Set Body: client_id: Client ID from Azure AD client_secret: Client ID from
      Azure AD username: username of the crm user password: crm user's password resource: crm url eg: https://alphac25.crm5.dynamics.com grant_type: password
    1. Add an action to Parse the HTTP response.
      image013
    1. Add another HTTP action to call CRM WebAPI:
      image014
      Note*:
      Authorization: Bearer <access_token>
      Accept: application/json
      OData-Version: 4.0
      Cache-Control: no-cache

Here is my test result:

  1. image015

Ray Wang From APAC DSI team.