Authorization type Bearer Token on Postman - authorization

I'm trying test a few endpoints using Postman.
All endpoint, require a token which can be obtain by log-in.
So I did this :
Request #1
After login success, I have access to the token from the response, then I store that token in my global variable.
let token = pm.response.json().location
console.log('Token : ', token.split("?token=")[1]);
pm.globals.set("token", token)
I need to use that token as Authorization type Bearer Token for my request #2.
I can copy & paste that in the token box, but I tried to avoid doing that manually, is there a way to do it automatically so I can run these 2 requests in sequence?

At first, create an environment ( top right corner of postman - image below ) This
is not a mandatory step by I suggest you to do for better handling of variables
I have modified the script to suit your need
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("ID", jsonData.Location.split("?token=")[1]);
Now this will export the value of the token ( screenshot below )
All you have to do next is to call the variable in request #2
By this you don't have to manually copy, paste into request #2 every single time

NO there isn't any till now. It has to be done manually if you want to have complete value or else you can store it in a variable and use that variable directly for the token.

Related

Unable to pass the JSESSIONID to the another scenario in karate [duplicate]

Scenario 1:
url: path1
Method : POST
Headers
Accept
Content-Type
Form fields:
Username
Password
url: path2
Method:get
Headers
Accept
Content-Type
Output of path2 is CSRF TOKEN and cookies having JESSION ID
Scenario2:
For calling any path in scenario 2 we need to pass the path2 outputs which is CXRF TOKEN and JSESSIONID
Problem:
For our test cases we need to always pass the jsession id(this jession id should be taken from the path2 only) and csrf token from path2 to all the subsequent calls. Which is not happening now
1>Csrf token is not getting passed to scenario2
2>JESSION ID created here is different from the jesession id from path2
Wants to know if there is way to pass this csrf token and jesession id headers to different scenario please?
First read the doc and then look at these particular examples: call-updates-config.feature and common.feature
Here's what I would do:
try to get everything working as a SINGLE scenario first before proceeding further. normally you never need to set cookies manually
write a re-usable feature that makes the calls to path1 and path2 and keep that in common.feature.
at the end of common.feature set the values of the CSRF token and JSESSIONID to variables
now you can write a second feature or scenario that calls common.feature. again, please read the docs, there are so many examples on how to call one feature from another and pass variables
as I said, you normally don't need to do anything to cookies, but you should be able to set the cookie like this (refer syntax for cookie): * cookie JSESSIONID = yourVariableName
similarly use the header keyword or whichever is appropriate for setting the CSRF token correctly

Karate Not getting response cookies when upgraded to 1.0.0 [duplicate]

Scenario 1:
url: path1
Method : POST
Headers
Accept
Content-Type
Form fields:
Username
Password
url: path2
Method:get
Headers
Accept
Content-Type
Output of path2 is CSRF TOKEN and cookies having JESSION ID
Scenario2:
For calling any path in scenario 2 we need to pass the path2 outputs which is CXRF TOKEN and JSESSIONID
Problem:
For our test cases we need to always pass the jsession id(this jession id should be taken from the path2 only) and csrf token from path2 to all the subsequent calls. Which is not happening now
1>Csrf token is not getting passed to scenario2
2>JESSION ID created here is different from the jesession id from path2
Wants to know if there is way to pass this csrf token and jesession id headers to different scenario please?
First read the doc and then look at these particular examples: call-updates-config.feature and common.feature
Here's what I would do:
try to get everything working as a SINGLE scenario first before proceeding further. normally you never need to set cookies manually
write a re-usable feature that makes the calls to path1 and path2 and keep that in common.feature.
at the end of common.feature set the values of the CSRF token and JSESSIONID to variables
now you can write a second feature or scenario that calls common.feature. again, please read the docs, there are so many examples on how to call one feature from another and pass variables
as I said, you normally don't need to do anything to cookies, but you should be able to set the cookie like this (refer syntax for cookie): * cookie JSESSIONID = yourVariableName
similarly use the header keyword or whichever is appropriate for setting the CSRF token correctly

Get new access token with Authorization Code OAuth2 - Using Robot framework

I have some troubles with getting Access token with grant type authorization code using Robot framework with Oauth2.
We use also a username/password authentication and after give the following parameters we get back our access token:
Grant Type, Callback URL, Auth URL, Access Token URL, Client ID, Client Secret, Scope, State.
I tried with RequestsLibrary and ExtendedRequestsLibrary as well, but no success so far.
Actually I do not know how to add parameters: callback url, auth url, access token url and state.
First try - using RequestsLibrary
Get admin token
&{HEADER_TOKEN}= Create Dictionary Content-Type=${CONTENT_TYPE}
&{DATA_TOKEN}= Create Dictionary token_name=backend_token grant_type=${GRANT_TYPE} redirect_uri =${CALLBACK_URL} auth_url=${AUTH_URL} access_token_url=${ACCESS_TOKEN_URL} client_id=${CLIENT_ID} client_secret=${CLIENT_SECRET} scope=${SCOPE} state=${STATE} username=${USERNAME} ${PASSWORD}
${BACKEND_RESPONSE}= RequestsLibrary.Post Request ${BACKEND_SESSION} /oauth/token data=${DATA_TOKEN} headers=${HEADER_TOKEN}
Log to console ${BACKEND_RESPONSE}
Should Be Equal As Strings ${BACKEND_RESPONSE.status_code} 200
Second try - using ExtendedRequestsLibrary
Get brand new admin token
${SESSION_RESPONSE}= Create Password Oauth2 Session client ${TOKEN_URL} ${CLIENT_ID} ${CLIENT_SECRET} ${USERNAME} ${PASSWORD} base_url=${BASE_URL}
&{HEADER_TOKEN}= Create Dictionary Content-Type=${CONTENT_TYPE}
&{DATA_TOKEN}= Create Dictionary token_name=client grant_type=${GRANT_TYPE} callback_url=${CALLBACK_URL} auth_url=${AUTH_URL} access_token_url=${ACCESS_TOKEN_URL} client_id=${CLIENT_ID} client_secret=${CLIENT_SECRET} scope=${SCOPE} state=${STATE}
${BACKEND_RESPONSE}= ExtendedRequestsLibrary.Post Request client /oauth/token data=${DATA_TOKEN} headers=${HEADER_TOKEN}
Log to console ${BACKEND_RESPONSE}
Should Be Equal As Strings ${BACKEND_RESPONSE.status_code} 200
Log to console ${BACKEND_RESPONSE.status_code}
If you have any idea just let me know.
thx!
using RequestsLibrary try with this approach it should work:-
Create Session baseUri https://xxxxxx.xx.xxx/xxx/xx verify=True
&{params}= Create Dictionary client_id=${client_id} client_secret=${client_secret} grant_type=${grant_type}
&{headers}= Create Dictionary Content-Type=application/json
${resp}= Post Request baseUri /oauth/token none none ${params} ${headers}
Log to Console ${resp.json()['access_token']}
Status Should Be 200 ${resp}
you are passing data=${DATA_TOKEN} as a body in your post request. You need to send it as query params. First parameter will be alias 2nd is uri 3rd is data 4th is Json and 5th is query params so in
Post Request baseUri /oauth/token none none ${params} ${headers}
you will find 3rd and 4th parameter as none. Hope this works

intermittent error from rally 'Not authorized to perform action: Invalid key' for POST request in chrome extension

I developed a chrome extension using Rally's WSAPI v2.0, and it basically does the following things:
get user and project, and store them
get current iteration everytime
send a post request to create a workitem
For the THIRD step, I sometimes get error ["Not authorized to perform action: Invalid key"] since end of last month.
[updated]Error can be reproduced everytime if I log in Rally website via SSO before using the extension to send requests via apikey.
What's the best practice to send subsequent requests via apikey in my extension since I can't control end users' habits?
I did see some similar posts but none of them is helpful... and in case it helps:
I'm adding ZSESSIONID:apikey in my request header, instead of user /
password to authenticate, so I believe no security token is needed
(https://comm.support.ca.com/kb/api-key-and-oauth-client-faq/kb000011568)
url starts with https://rally1.rallydev.com/slm/webservice/v2.0/
issue is fixed after clearing cookies for
https://rally1.rallydev.com/, but somehow it appears again some time
later
I checked the cookie when the issue was reproduced, and found one with name of ZSESSIONID and its value became something else rather than the apikey. Not sure if that matters though...
code for request:
function initXHR(method, url, apikey, cbFunc) {
let httpRequest = new XMLHttpRequest();
...
httpRequest.open(method, url);
httpRequest.setRequestHeader('Content-Type', ' application\/json');
httpRequest.setRequestHeader('Accept', ' application\/json');
httpRequest.setRequestHeader('ZSESSIONID', apikey);
httpRequest.onreadystatechange = function() {
...
};
return httpRequest;
}
...
usReq = initXHR ('POST', baseURL+'hierarchicalrequirement/create', apikey, function(){...});
Anyone has any idea / suggestion? Thanks a million!
I've seen this error when the API key had both read-only and full-access grants configured. I would start by making sure your key only has the full-access grant.

Sagepay token system delete token

I'm using Sage Pay token system, evrithyng is working fine I store the tokens on my side. The question is if I want to remove a token is it fine to remove it only from my side, and then if some one wants to add tha card again to create another token or I have to send a request to sagepay with StoreToken = 0 param?
You can remove the token from your side only, but I would recommend sending a REMOVETOKEN request to Sage Pay to remove it (or setting StoreToken=0 on usage) - token storage is charged above a certain threshold. No point paying for something you can't use.....
Your end user can create another token if required.
Here goes the code:
# REMOVE TOKEN REQUEST
if(YourCondition=='OK')
{
$strRemoveTokenURL = "https://test.sagepay.com/gateway/service/removetoken.vsp";
$sToken = 'Token Stored your side';
$strPost = "VPSProtocol=3.00&TxType=REMOVETOKEN&Vendor=yourvendorid&Token=".$sToken;
$arrRemoveResponse = requestPost($strRemoveTokenURL, $strPost);
}
# REMOVE TOKEN RESPONSE
echo '<hr>';
print"<pre>";print_r($arrRemoveResponse);print"<pre>";
exit;