I have recently started to learn Rest Api automation testing using RestSharp. I was able to perform basic test for some rest api. However for the same test as part of new build developers have added authorization feature which is Oauth2. I am not sure how and where to implement it. I have attached very basic get test and some information which might be need for you to help me.
using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Windows.Input; using System.Windows.Forms; using System.Drawing; using Microsoft.VisualStudio.TestTools.UITesting; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UITest.Extension; using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard; using RestSharp; using System.Net; using RestSharp.Authenticators; namespace RestApiTest { [CodedUITest] public class TestApi { [TestMethod] public void TestGetApi() { //https://XYZtestsample.azurewebsites.net/ - previous link, without authorization it works fine when passed as Restclient argument var client = new RestClient("http://XYZarsuitewswagger-XYZarsuitestaging.azurewebsites.net/"); //--- This one has authentication header and does not work. //HttpBasicAuthenticator obj = new HttpBasicAuthenticator("XYZ.XYZ@in.XYZ.com", "XYZ@2019"); var request = new RestRequest("/XYZARSuiteAPI/v1/BUProfiles", Method.GET); // request.AddHeader("XYZ.XYZ@in.XYZ.com", "XYZ@2019"); request.RequestFormat = DataFormat.Json; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; //new OAuth2Authenticator("XYZ.XYZ@in.abb.com", "XYZ@2019"); IRestResponse response = client.Execute(request); var content = response.Content; }
Below information I can see in Rest Api
UPDATE: I have authorize option available in the sample UI given to check the output when I authorized it I got below info
curl -X GET " http://XYZarsuitewswagger-XYZarsuitestaging.azurewebsites.net/XYZARSuiteAPI/v1/BUProfiles " -H "accept: application/json" -H "authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1zeE1KTUxDSURXTVRQdlp5SjZ0eC1DRHh3MCIsImtpZCI6Ii1zeE1KTUxDSURXTVRQdlp5SjZ0eC1DRHh3MCJ9..rJ1VDhjE3NDMnW5pdv8N02OsKX3vKBO0bTxo2Lx_KZWq6eMi1DAvTXNd49tZhCJTr-PrCwbwsZsvf3fye_fNjL2VthlOA40UsvbPkou-F_b3B2kK3jzAdn8icbNEw5-3cxx8nAKiuDVZeRMTa3KVj0Kd384Eb6ZO5JBf4evoMCNMidrVAeKbxcP0B2tF8Aw1cgDWswNqghCUjb9foxxVIP-7MFeNFpuxGvLjtcQHIgY9VqMMpYBpE5DYLjqX0yXkGqBWnD4v-590wYtH0YPB-XOOtKZsy280PEHMock8jlLQdBNOp0w9SVw7XLfy5p0vp3ovHZqbb9IbSHQy-frV4g"
Thank you.
You have to set Authenticator property on RestClient object. Also it is important to add Accept header on the RestRequest object, not sure if it would be json in your case, but you can set here any valid content type.
Sample:
var client = new RestClient("<your_base_url>");
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator("<your_access_token>", "Bearer");
var request = new RestRequest("<your_query>", Method.GET);
request.AddHeader("Accept", "application/json");
IRestResponse response = client.Execute(request);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.