How to Save Bearer Token for future Use using RestSharp - rest

Im a relative noob to automated testing - currently, I'm using Postman to generate RestSharp code for API requests in Visual Studio 2017 Enterprise.
Esentially, I'm creating a basic unit test, then plopping in the code to execute the test
What I need to know is this - is it possible for me in my tests to first do a login call and SAVE my bearer token in order to use in in all subsequent calls - in postman, this is easy because I can just set an environmental variable that pulls it in.
So essentially, what I need to do is make a login call to return the bearer token
save the bearer token, then use that token in the subsequent calls I make
Any help is GREATLY appreciated!

A bit late, but here is how I does it:
Use RestSharps JwtAuthenticator. Get your access token the way you already do. Next create the JwtAuthenticator and finally use that authenticator in your RestSharp calls:
var accessToken = GetAccessToken(); // Does what ever is required to get the acces token
Authenticator = new JwtAuthenticator(accessToken);
...
var client = new RestClient("http://example.com");
client.Authenticator = Authenticator;
...
// All requests will be correctly authenticated
client.Execute(requestA);
client.Execute(requestB);
client.Execute(requestC);
....

Related

storing and sending jwt httponly cookie, and csrf token with postman

I have a flask API, with jwt authentication, on a httponly cookie. I installed interceptor, added the domain(with HTTPS) to the list, and enabled the requests and cookies interception.
but still,
how do I make postman send the cookie I got from logging in to the server? usually, with a simple front-end, it just happens, so I didn't think about it.
all the methods I found in postman documentation, including specifying the value with the token, but I don't have it, since I can't access the httponly cookie. (or can I?)
must I access the cookies? can it be done automatically like simply sending requests from the front-end?
any guidance will be appreciated
After a full evening of research, I did two things to make it work -
in the login request, I added a "test" script(a post-request script in postman), with the following code:
const csrf_token = pm.response.headers.get("set-cookie");
const edited_token = csrf_token.split(/[;=]/)[1];
pm.environment.set("X-CSRF-TOKEN", edited_token);
console.log(csrf_token.split(/[;=]/)[1]);
First, I got the cookie from the response, and then used a regex to separate only the token value, and set it as an environment variable. this way, I could add it as a header later, for accessing protected URLs.
The second step was to add a pre-scrit in any request with a protected URL -
in the pre-request tab, I added the following:
pm.request.headers.add({
key: 'X-CSRF-TOKEN',
value: pm.environment.get("X-CSRF-TOKEN")
});
Which only added the same token I took earlier from the "X-CSRF-TOKEN" environment variable and set it to the header.
Mission accomplished :)
I hope it will help others who bumped into this

How to get cookies from GET API using HTTPClient and extract value from them

I am using httpclient to automate rest api Get calls and in same I am getting cookies that contains csrf token that I need to login into my project , but I am not able to find out the way how to do so.
just need to print closeableHttpResponse , it will give you all responses and then get the cookie value like this
String value = closeableHttpResponse.getHeaders("Set-Cookie")[0].getValue();

How to add Oauth Bearer token in QTP

I'm working on HP UFT Rest API Testing where i need to Pass "Authorization : Bearer " to perform or execute the Rest Service request then only my service will execute and displays the response.
Can any one give me the solutions,How to add Oauth Bearer token in QTP for Rest API Testing.
Please refere below link :
https://admhelp.microfocus.com/uft/en/14.03/UFT_Help/Content/User_Guide/task_API_coding_manipulate_WSC_properties.htm
There will be methods associated with the every block/methods , you can write C# code to append the string to the output of authorization operation and store in a global variable- that can be passed as a input to subsequent operation.
BeforeMethod , aftermethods are some of Event Handlers that are assosicated with each operation.

magento REST API not accessible in iphone

When I try to access rest API using iPhone I have passed following parameter in URL and used both methods POST and GET but it displays Access Denied.
oauth_version="1.0",
oauth_signature_method="HMAC-SHA1",
oauth_nonce="B0dlzkfMWCAn0TJ",
oauth_timestamp="1366280813",
oauth_consumer_key="klkjylsiozbv6vfdsqtuheqo3kmqqzv2",
oauth_token="t9pefrwylmg7webyepsqepotlhzbytkp",
oauth_signature="NeOwbCLUPbIyF9ErnHoFQOl9%2Bwo%3D"
I have worked with REST Client plugin available for Firefox and Chrome, REST API is work well using REST Client plugin but not accessible in iPhone.
I am generating a random value for oauth_timestamp, oauth_signature and oauth_nonce then also REST API is displaying Access Denied.
Please provide suggestions.
//here final_sign is signature generated from following procedure.
$nonce = substr(md5(uniqid('nonce_', true)),0,16);
$temprealm="http://magentohost/api/rest/products";
$realm=urlencode($temprealm);
$oauth_version="1.0";
$oauth_signature_method="HMAC-SHA1";
$oauth_consumer_key="dfddfgdgdfgddf6qgygmyvw7e3";
$oauth_access_token="fdgdfgfdgdfg357gimpdnuejvcbtk51ni";
$oauth_method="GET";
$oauth_timestamp=time();
$algo="sha1";
$key="sb88hfdihyg25ipt1by559yzbj2m3861&s7uhaheu8nrx961oxg6uc3os4zgyc2tm"; //consumer secret & token secret //Both are used in generate signature
$data="oauth_consumer_key=".$oauth_consumer_key."&oauth_nonce=".$nonce."&oauth_signature_method=".$oauth_signature_method."&oauth_timestamp=".$oauth_timestamp."&oauth_token=".$oauth_access_token."&oauth_version=".$oauth_version;
$send_data=$oauth_method."&".$realm."&".urlencode($data);
$sign=hash_hmac($algo,$send_data,$key,1); // consumer key and token secrat used here
$fin_sign=base64_encode($sign);
echo $fin_sign;
From your question I understand that you use a random value for the signature and the nonce.
The latter would be fine, but a random signature would lead the receiver not to trust you as a legitimate client.
So, actually, you get the response you requested (;-)). But that does not solve your problem.
You have to generate a valid signature for the magento system.

Authentication for twitter API and Rest Call

I have been using FB api for some simple demo and everything was quite easy with the authentication. Now I have to do something similar with twitter v1.1 but there is something that I really don't understand...
Example:
I want to do this request:
https://api.twitter.com/1.1/search/tweets.json?q=q=%23freebandnames
the problem is that I have to be authenticated, anyone have some examples? I don't want to create a twitter connection because I don't need different users to be connected to my applicaiton. I have just to perform some simple search request but I can't understand how to use the authentication parameters. Which type of Ajax request I have to use in order to perform the REST request authenticated??? (Obviously I have my secret token and my access secret token) but how to use them????
THanks in advance for answers
You can use this javascript library: codebird-js with the "Application-only auth".
or
Do it yourself: everything is explained in the documentation.
Basically you need to follow 3 steps (and you should do the 2 first just once):
An application encodes its consumer key and secret into a specially encoded set of credentials.
An application makes a request to the POST oauth2/token endpoint to exchange these credentials for a bearer token.
When accessing the REST API, the application uses the bearer token to authenticate.
The steps are detailed in the documentation.
You can do the first 2 separately and when you get your bearer token, you need to add a specific HTTP Header (Authorization) in each request with your bearer token (that's the 3rd step). With jQuery it could be something like that:
$.ajax({
headers: { Authorization: 'Bearer '+$my_bearer_token },
url: 'https://api.twitter.com/1.1/search/tweets.json?q='+$search
}).done(function (data) {
// Play with the data
});