Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Discovery web API in CRM online return error 401 - Unauthorized

Posted on by 1,730

Hi,

I am facing this issue to get instance data through this API https://disco.crm.dynamics.com/api/discovery/v9.1/Instances but Everytime I hit in Browser or Postman I get this error 

401 - Unauthorized: Access is denied due to invalid credentials.

You do not have permission to view this directory or page using the credentials that you supplied.

Can you guys Help me for this?

Thanks in advance.

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    As I am creating app I don't think we can use the user credentials in that . We have to use client id and secret id . May be I am wrong but how we authenticate to user before doing this .

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    Hi Ajyendra,

    Main:

    static void Main(string[] args)
    {
    
        try
        {
            String token = Authenticate().Result.AccessToken;
    
            Task.WaitAll(Task.Run(async () => await GetInstances(token)));
        }
        catch (Exception e)
        {
            Console.WriteLine("Oh..an error occured..:\n "   e);
        }
    
    }

    Function to acquire token:

    private static async Task Authenticate()
    {
    
        string userName = "";
        string password = "";
        string clientId = "";
        string adminUrl = "https://admin.services.crm5.dynamics.com/";
        var credentials = new UserPasswordCredential(userName, password);
        var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/common/");
        var result = await authenticationContext.AcquireTokenAsync(adminUrl, clientId, credentials);
        return result;
        
    }

    Test for getting instances information:

    private static async Task GetInstances(String token)
    {
    
        var httpClient = new HttpClient
        {
            BaseAddress = new Uri(Resource),
            Timeout = new TimeSpan(0, 2, 0)
        };
    
        httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
        httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    
        var retrieveResponse =
        await httpClient.GetAsync("/api/v1.2/instances");
    
        if (retrieveResponse.IsSuccessStatusCode)
        {
            var jRetrieveResponse =
            retrieveResponse.Content.ReadAsStringAsync().Result;
            Console.WriteLine("Your instances retrieved from Office 365 tenant: \n{0}", jRetrieveResponse);
            //var result = JArray.Parse(jRetrieveResponse);
            //foreach (var data in result)
            //    Console.WriteLine("Id: "   data["Id"]   "\nUnique Name: "   data["UniqueName"] "\n");
        }
        else
        {
            Console.WriteLine("The request failed with a status of '{0}'",
                   retrieveResponse.ReasonPhrase);
        }
        
    }

    Regards,

    Clofly

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    It will return same result if I used different Resource url in postman. cloflyMao

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    Yes

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    Hi Ajyendra,

    Do you mean C# code?

    Regards,

    Clofly

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    Hi cloflyMao,

    IT would be great help .

    Can you share the code ? When I tried this with code it gave me error 401 or 403 error .

    Thanks in advance

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    Thanks @Clofly I appreciate your help.

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    Hi

    At least one role is required.

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/online-management-api/get-started-online-management-api#office-365-admin-roles

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    YEs. If I don't have a Global asdmin or Service Administrator Rights . Can I accessible that data?

    pastedimage1577947016602v1.png

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Discovery web API in CRM online return error 401 - Unauthorized

    hi

    have you requested a new token based on admin service url?

    pastedimage1577946702593v1.png

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans