Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Accessing on premise web Api through postman gives 401 - Unauthorized error

(0) ShareShare
ReportReport
Posted on by 505

I'm trying to make a get request to on premise web Api through postman and getting 401- Unauthorized error.

Is is related to security role/permission? Which security role is needed to make a call through web Api?

I'm using basic authentication. 

The below code is also giving the same error.

private HttpClient getNewHttpClient(string userName,string password,string domainName, string webAPIBaseAddress)  
{  
    HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(userName, password, domainName) });  
    client.BaseAddress = new Uri(webAPIBaseAddress);  
    client.Timeout = new TimeSpan(0, 2, 0);  
    return client;  
}  

HttpRequestMessage request = null;
try
{
request = new HttpRequestMessage(HttpMethod.Post, “api/data/v8.0/accounts”); //uri to accounts
request.Content = new StringContent(content.ToString()); //JObject of the data to be posted.
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(“application/json”);

//Send the HttpRequest

Task<HttpResponseMessage> response = httpClient.SendAsync(requestMessage);

//Wait till the Response Execution is complete
response.Wait();

//If the response is Successfully executed then it will return the value true
if (response.Result.IsSuccessStatusCode)
{
}
}
catch (Exception err)
{
throw new Exception(err.Message);
}


  • C. Jensen Profile Picture
    384 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    Hi Raki

    As ClientId you can use a random guid.

    The RedirectUri depends on the purpose. If you are using the client in Postman, then you do not need it. It it used to loopback to the client in the authentication process.

    /Christian

  • Raki90 Profile Picture
    15 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    Christian Jensen Could you please tell us what should be the client id and and RedirectUrl to run this script? 

  • Musau Profile Picture
    5 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    IFD

  • Verified answer
    C. Jensen Profile Picture
    384 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    Sure. On your ADFS server, run the following PowerShell commands:

    Add-AdfsClient -Name "My Custom Application Name" -ClientId  e5cf0024-a66a-4f16-85ce-99ba97a24bb2  -RedirectUri http://localhost/SdkSample
    Grant-AdfsApplicationPermission -ClientRoleIdentifier e5cf0024-a66a-4f16-85ce-99ba97a24bb2 -ServerRoleIdentifier mydomain.crm.dynamics.com
    

    I have used ClientId, ClientRoleIdentifier and RedirectUri from Microsofts example.

    ServerRoleIdentifier needs to be the identifier of your relying party trust.

    If you need your application to access both internal and external relying party, then set the argument to "-ServerRoleIdentifier https://internal.crm.dynamics.com,https://external.crm.dynamics.com

    You need to modify the arguments, to reflect your environment and application.

    Hope this solves your problem.

    /Christian

  • Priyank Bhavsar Profile Picture
    505 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    Thanks Christian. Can you please share the link to register ADFS client?

  • C. Jensen Profile Picture
    384 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    Hi

    You need to use OAuth.

    Microsoft has an example here: Connect to Microsoft Dynamics 365 web services using OAuth

    Remember to register the ADFS client. Otherwise your application will fail to connect.

    /Christian

  • Priyank Bhavsar Profile Picture
    505 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    It is IFD. Does basic authentication work with IFD? I believe I need to use OAuth2.

  • C. Jensen Profile Picture
    384 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    Have you enabled Internet-Facing deployment or Claims-Based Authentication?

  • Priyank Bhavsar Profile Picture
    505 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    Hi Christian,

    I'm using correct version in the URL. Based on error description it seems that it is a permission issue.

  • C. Jensen Profile Picture
    384 on at
    RE: Accessing on premise web Api through postman gives 401 - Unauthorized error

    Hi

    Is your instance running version 8.0? Or have you updated to 8.1 or 8.2? Because then you nede to correct the version in the url.

    /Christian

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,110 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans