Skip to main content

Notifications

Dynamics 365 general forum
Unanswered

Testing WebAPI via RedirectURL

Posted on by 10

Hi,

I'm developing a web service that will connect to my Dynamics 365 instance. I've set up localhost as a valid web redirect URI, yet when I call my web service, I am still getting

 No connection could be made because the target machine actively refused it 52.xxx.xxx.xx:443

Am I missing something obvious or do I need to publish this onto a server instead of localhost - despite what the docs say.

Specifically, I believe I'm referring to the Azure AD redirect but so that it works with the Dynamics CRM.

https://docs.microsoft.com/en-us/azure/active-directory/develop/reply-url

I want a separate Web service to communicate with my Dynamics 365 instance.

TIA

  • akselsoft Profile Picture
    akselsoft 10 on at
    RE: Testing WebAPI via RedirectURL

    Any luck? Or can I give you anything further?

  • akselsoft Profile Picture
    akselsoft 10 on at
    RE: Testing WebAPI via RedirectURL

    This is a screen shot of the AD page where our admins have added localhost for testing purposes.

    Dynamics-Configuration.png

  • akselsoft Profile Picture
    akselsoft 10 on at
    RE: Testing WebAPI via RedirectURL

    I've done it two ways but the most recent version looks like this:

    Get Token Code

    var formContent = new FormUrlEncodedContent(new[]
    {
    new KeyValuePair<string, string>("resource", Resource),
    new KeyValuePair<string, string>("client_id", ClientId),
    new KeyValuePair<string, string>("client_secret", ClientSecret),
    new KeyValuePair<string, string>("grant_type", "client_credentials")
    });

    HttpResponseMessage response = await httpClient.PostAsync(Authority, formContent);

    return !response.IsSuccessStatusCode ? null
    : response.Content.ReadAsStringAsync().Result;

    Web Service Call

    using (HttpClient httpClient = new HttpClient())
    {
    httpClient.BaseAddress = new Uri(Resource);
    httpClient.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", _accessToken);


    //Retrieve
    HttpResponseMessage retrieveResponse =
    await httpClient.GetAsync($"api/data/v8.2/entityName?$select=fieldName&$filter=contains(fieldName,'{sParameter}')");
    if (retrieveResponse.IsSuccessStatusCode)
    {
    JObject jRetrieveResponse =
    JObject.Parse(retrieveResponse.Content.ReadAsStringAsync().Result);
    JObject row = (JObject)JArray.Parse(jRetrieveResponse["value"].ToString())[0];

    string sGuid = row["core_fc_profileid"].ToString();
    sURL = string.Format("{0}/main.aspx?etn={1}&pagetype=entityrecord&id=%7B{2}%7D",
    "CRM URL", "core_fc_profile", sGuid);
    }
    else
    {
    var result = await retrieveResponse.Content.ReadAsStringAsync();
    }

    return sURL;
    }

    The original code was more like:

    DataCollection<Entity> retrievedFile  =
    CRMCommonTools.OrgConnection.RetrieveMultiple(queryQueuePrivileges).Entities;
    if (retrievedFile.Count == 1)
    {
    sCaseFileID = retrievedFile[0].Id.ToString();
    return sCaseFileID;
    }
    else
    throw new Exception(String.Format("Cannot find unique File for ID = {0}", sCaseFileName));

    }

    All of them cause the same problem but all WORK when called from a Windows/desktop app.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Testing WebAPI via RedirectURL

    Hi akselsoft,

    Could you provide the code you used?

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

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,522 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,441 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans