microsoft-teamsmicrosoft-dynamicsmicrosoft-graph-teamsdynamics-crm-webapiteams-toolkit

Use teamsfx to call dynamics global discovery service


I am trying to use teams to call the dynamics global discovery API on my react tab teams app.

I can get it working on postman following the guide here: https://learn.microsoft.com/en-gb/power-apps/developer/data-platform/discovery-service?WT.mc_id=ppac_inproduct_legacy however, when I try to call the API through teamsfx on my teams react app I am getting nothing back, but I am getting a 200 success code. I have implemented the the call as below.

  await teamsUserCredential.login(["User.Read","https://globaldisco.crm.dynamics.com/user_impersonation"]);
  const accessToken = await teamsUserCredential.getToken("https://globaldisco.crm.dynamics.com/user_impersonation");
  const response = await fetch('https://globaldisco.crm.dynamics.com/api/discovery/v2.0/Instances', { 
    headers: { 
      Authorization: `Bearer ${accessToken.token}`, 
    }
  });

I have also granted admin permission on the app registration, however am I right in saying this is not technically necessary as the login function should ask for permission at a user level?

enter image description here

Any help in getting me up and running so I can call the global discovery API would be much appreciated.


Solution

  • I tested using Postman and code, and it seems to be the fetch part.

    From postman:

    GET https://globaldisco.crm.dynamics.com/api/discovery/v2.0/Instances
    

    Postman Result

    After updating the fetch to code attached below, you should be able to get the data as shown in Postman:

    const accessToken = await teamsUserCredential.getToken("https://globaldisco.crm.dynamics.com/user_impersonation");
    const response = await fetch('https://globaldisco.crm.dynamics.com/api/discovery/v2.0/Instances', { 
      headers: { 
        Authorization: `Bearer ${accessToken!.token}`, 
      }
    }).then((response) =>{
      if (!response.ok) {throw new Error(`HTTP error! Status: ${response.status}`)}
      return response.json();
    }).then(data => {return data});
    

    Fetch Response