How to Consume WEB API in C# | C# Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i am going to show you how to call an existing web api from a c-sharp application we are going to call the spotify web api to display new releases on a new web page you can follow the same steps to call any web api my name is pat if you are new here consider subscribing let's get into it getting data from spotify api is a two-step process we need to get an access token with the token we can make requests to the web api to use the web api you need to create an account you can create one for free before requesting data we need to register the application this is not necessary if you are not using user data but there are limitations if you do not let's go to the spotify developer website in the dashboard section log in with your credentials let's create a new app the name is web api tutorial in the description field we add tutorial check all the boxes click on the create button to create the app once the app is created you can see the client id and the client secret we will need them for the authorization phase now let's create a new project in visual studio 2019 select asp.net core web application let's call it consume spotify web api in the next dialog we select asp.net core web app click on the create button to generate the project in c-sharp we use the http client class to make http requests there are many consumption patterns when you need to use the http client for this tutorial we are going to inject the http client into a service through dependency injection let's start by requesting an access token from the spotify account service endpoint we create a folder named services we had an interface called i spotify account service we declare a method that returns a string which will be the access token the string is wrapped in a task because we are going to call this method asynchronously the name is get token it takes as parameters the client id and the client secret these two are needed in the request let's create an implementation of this interface we add a new class called spotify account service which implements the i spotify account service we declare the http client inside the constructor for injection if we take a look at the documentation we need to make an http pass request to the following endpoint we need to provide an authorization parameter in the header we need to provide a body as a form url encoded let's translate this into c-sharp code the request is represented by an instance of http request message the first argument is the http verb in this case it's a post and the second argument is the uri we provide the string token which is the relative path the base address will be declared when registering the service with the dependency injection to provide the authorization in the header we set a new instance of the authentication header value class on the authorization property of the request header the first argument is the type of authentication in our case it's basic the second argument is a string that combines the client id and the client secret encoded in base 64. the request content takes an instance of the form url and coded content class it takes as an argument a dictionary of string as a key and a string as a value the key here is grant type and the value is client credentials the request object is done we send the request by invoking the send async method on the http client we call the ensure success status code on the response object to make sure our requests succeed otherwise an exception will be drawn now let's read the response we get the content of the response as a stream we need to deserialize this stream into an object we don't have that object yet let's create a new class called alt result this class will be the recipient of the response this class needs to be created according to the json result of the authentication we have a sample in the documentation let's convert it to a c-sharp object we copy the json content in visual studio we go to edit paste json as classes visual studio has created a new class let's rename it to alt result you can notice that the properties are not named according to c-sharp naming convention for the sake of simplicity we will leave it as is let's go back to the get token method we deserialize the stream into the alt result let's add a using because the stream is disposable we return the access token property the service is done let's register it with the dependency injection system let's go to the startup class in the configure service method we invoke the add http client with the i spotify account service and its concrete implementation we also configured the http client base address with this code the i spotify account service will be resolved with an instance of spotify account service the http client will be injected into the spotify account service the creation of the http client is done by the http client factory now we can test the request of an access token let's go to the home controller we declare the i spotify account service which will be injected at runtime in the index method which is called when the home view loads we invoke the get token method of the ispodify account service interface the method need the client id and the client secret let's declare them in the app settings we had a new section spotify and we copy and paste the client id and the client secret here let's go back to the home controller let's add the i configuration interface which will allow retrieving configuration settings let's add the client id and the client secret from the app settings as arguments let's run the app the breakpoint is hit let's inspect the token variable yes it's filled with the token access now we can request new releases from the spotify web api let's create another interface called i spotify service this interface has a single method that returns a list of release we will create this class later the name of the method is get new releases it has three parameters the first two will help filter the request the country code the limit and the last one is the access token for authorization let's create the release object it has a name rs date of release an image and a link that will open the spotify webpage let's create an implementation of the ispodify service we call it spotifyservice we declare the http client in the constructor it will be injected at runtime in the get new releases method we are going to add an authorization header to the http client using an instance of the authentication header value class this is a bare token we pass the access token we need to make an http get request we invoke the get async method on the http client the method takes the endpoint uri as an argument we use a relative uri we will set the base url when registering with the dependency injection system we make sure the requests succeed now we need to deserialize the response stream into a c-sharp object let's try the request in the spotify console so we can copy the json result so this is the result of the request let's copy it in visual studio we create a new class called get new release result we paste json as classes all the classes needed to deserialize the json object are created let's rename the root object we go back to the get new releases method with the serializer response stream into an instance of get new release result the method signature states that the return type is a collection of release objects so in the class get new release result that corresponds to the item class so let's transform the collections of the items to a collection of release object using linked object the service is ready let's register it with the dependency injection system we invoke the add http client method on the service collection we configure the base address of the http client we add another default editor that says that we expect a json as a return type now we can test retrieving new releases let's go to the home controller we declare the i spotify service in the constructor in the index method we invoke the get new releases method we pass the country code of friends so we can get new releases from friends we set the limit of the records to 20 and we pass the access token let's run the application the breakpoint is hit so let's inspect the new releases variable okay we got some data now let's display the result in a view let's refactor the following code we put the code inside a method called get release if there is an error during the call we return an empty list of release object we pass the release list in the view and let's edit the ohms view this is a strongly type view so we declare the model as a ni enumerable of release we iterate over the model which is the list of release and we display each release inside a card we show the artist the name the release date the image and a link that will open the spotify web player in a new tab let's test the application now we have a view that displays new releases from spotify that's it for this tutorial if you like it feel free to like the video and subscribe to the channel thanks for watching see you soon you
Info
Channel: Tech With Pat
Views: 58,765
Rating: undefined out of 5
Keywords: .net, .net csharp tutorial, .net tutorial, asp.net core web api, asp.net core web api tutorial, asp.net web api, asp.net web api tutorial, c sharp tutorial, c# .net, c# rest api, c# tutorial, csharp, csharp for beginners, httpclient c#, rest api c#, tutorial, web api, web api tutorial c
Id: LZJvdFDCKxM
Channel Id: undefined
Length: 14min 31sec (871 seconds)
Published: Sun Feb 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.