Open Closed

How can I call an ABP remote service method? #560


User avatar
0
alper created
Support Team Director

I want to login to my ABP project and execute an authenticated Rest API action with the token.


5 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    In your solution directory, there's a folder called test. In the test folder, you'll find *.HttpApi.Client.ConsoleTestApp project. This project is a sample console application that logins to the backend and makes an authenticated API call. To be able to run this project, run the following projects

    • *.HttpApi.Host
    • *.IdentityServer

    Configure your host credentials in the appsettings.json of this console application:

    Run in debug, to make a request for fetching all users. (it's an authenticated request and adds bearer token automatically)

    If you want to manually build your own API client you can follow the IDS4 documentation: https://identityserver4.readthedocs.io/en/latest/quickstarts/1_client_credentials.html#creating-the-client


    On the other hand, you can also check out the ABP CLI code as an example. It authenticates to the backend and make API calls.

    1. Login => https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginCommand.cs#L51
    2. Set Bearer token => https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClient.cs#L48
    3. Make a remote service all => https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/Services/AbpNuGetIndexUrlService.cs#L22
  • User Avatar
    0
    shobhit created

    Hello Alper, Thanks but as i am new in ABP and Identity so i am confused. I am providing basic details: 1- Login using microsoft authtentication as per screen shot:

           2- Received auth token in angular UI           
           3- Call ABP custom api (workflowAPI to give the name)
           4- Pass auth token to calling api in header as "Authorization"
           5- Get the auth token in ABP workflowAPI api as below code:
       
    
        
               Declare in class:
               private readonly IHttpContextAccessor _httpContextAccessor;
               
               Within Method:
               var accessToken = _httpContextAccessor.HttpContext.Request.Headers["Authorization"];
               
                var client = new RestClient(url);           
                client.AddDefaultHeader("Authorization", accessToken);
                client.AddDefaultHeader("Content-Type", "application/json");
                client.AddDefaultHeader("Accept", "application/json");
                
                RestRequest restRequest = new RestRequest();
    
                var response = client.Execute<T>(restRequest);
                
    
            Error:
            "Unauthorized"
            
            Reason:
            "accessToken" is an  ABP identity server auth token not the Microsoft access token.
            
            
            Help Required:
            How to get the microsoft access token in my method. Can you give me step by step details.
    
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi @shobhit

    Is your purpose to log in through Microsoft or other social accounts in Angular?

  • User Avatar
    0
    shobhit created

    Hi Maliming, Ok. Scenario is: 1- User login using microsoft account (Done) 2- User view menu (Done) as per permission 3- User have one menu option "Workflow service". 4- User clicks the "workflow service" menu 5- Backend API "WorkflowserviceList" get triggered 6- Within "WorkflowserviceList" a microsoft api i.e. (GetWorkflowInstances) is called 7- To call microsoft api "GetWorkflowInstances", caller should pass microsoft access token

                  ABP API: 
                  
                  Public WorkflowserviceList()
                  {
                             Call microsoft API GetWorkflowInstances(Access Token);                             
                  }
                  
                  Access token should be of the same user who has logged in.
                  
                  I hope i am clear now.
    
  • User Avatar
    -1
    maliming created
    Support Team Fullstack Developer

    We can't provide much help on the question of calling Microsoft APIs. I recommend that you check Microsoft's document. : )

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11