title: How to discover the URL for my organization in Dynamics 365 CRM (online) using Web Api
body: <p>I cannot connect Dynamics CRM online to discover organization URL using a Web API. The sample code from Microsoft is not working. </p>
  
  <p>I am developing a console application, and want to connect to CRM-online using Web API(we are not using Organization Service since we would like to move to Web API), In the application, I need to discover all available organizations and then query the data from each organization. I am using the example from Microsoft, but it didn't work as expected. <a href="https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/samples/global-discovery-service-csharp" rel="nofollow noreferrer">Here is sample code</a>.</p>
  
  <p>I followed the Quick Start guide in the Azure AD. using MSAL.NET. </p>
  
  <p><a href="https://github.com/Azure-Samples/active-directory-dotnetcore-daemon-v2" rel="nofollow noreferrer">Active Directory Dotnet Core Daemon</a></p>
  
  <p>I tried both <a href="https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/discover-url-organization-web-api#how-to-access-the-discovery-services" rel="nofollow noreferrer">global discovery URL and data center discovery URL</a>, same fault.</p>
  
  <p>Here are my code using MASL.NET.</p>
  
  <pre><code>static void Main(string[] args)
  {
  
      IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create("3f4b24d8-61b4-47df-8efc-1232a72c8817")
          .WithClientSecret("of+Ozx_ARRX?ONj+QCzWXf84eTQABA17")
          .WithAuthority("https://login.microsoftonline.com/common/oauth2/authorize", false)
          //.WithAuthority("https://login.microsoftonline.com/3a984a19-7f55-4ea3-a422-2d8771067f87/oauth2/authorize", false)
          .Build();
  
  
      var authResult = app.AcquireTokenForClient(new String[] { "https://disco.crm5.dynamics.com/.default" }).ExecuteAsync().Result;
      //var authResult = app.AcquireTokenForClient(new String[] { "https://globaldisco.crm.dynamics.com/.default" }).ExecuteAsync().Result;
  
      //var authResult = app.AcquireTokenForClient(new String[] { "https://crm525842.crm5.dynamics.com/.default" }).ExecuteAsync().Result;
  
      HttpClient client = new HttpClient();
      client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
      client.Timeout = new TimeSpan(0, 2, 0);
      client.BaseAddress = new Uri("https://disco.crm5.dynamics.com/");
      //client.BaseAddress = new Uri("https://crm525842.api.crm5.dynamics.com");
  
      HttpResponseMessage response = client.GetAsync("api/discovery/v9.1/Instances()", HttpCompletionOption.ResponseHeadersRead).Result;
      //HttpResponseMessage response = client.GetAsync("api/data/v9.1/WhoAmI()", HttpCompletionOption.ResponseHeadersRead).Result;
  
      if (response.IsSuccessStatusCode)
      {
         //Get the response content.
          string result = response.Content.ReadAsStringAsync().Result;
      }
      else
      {
          //401 occurred
          throw new Exception(response.ReasonPhrase);
      }
  }
  </code></pre>
  
  <p>I can get the access token, I expect this work but the response is: </p>
  
  <pre><code>{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
  {
    X-CrmServer: HK2CRMWOSDISW11
    Strict-Transport-Security: max-age=31536000; includeSubDomains
    Date: Mon, 05 Aug 2019 05:47:12 GMT
    Set-Cookie: crmf5cookie=!DTRY0s4VpQxjLnMqwoaZ9AdJzXkdU6Vfto/+0KmA1KJeDxXIK15WxYQUdDi6BwLXgIrzskdvtk0u7A==;secure; path=/
    Server: Microsoft-IIS/8.5
    WWW-Authenticate: Bearer error=invalid_token, error_description=Error during token validation!. Ticket id feef1832-8a12-4d3d-a426-099176943f35, authorization_uri=https://login.microsoftonline.com/common/oauth2/authorize, resource_id=https://disco.crm5.dynamics.com/
    Content-Length: 1293
    Content-Type: text/html
  }}
  </code></pre>