Im trying to automate postman tests with Newman. There is an issue with authorization. The authorization bearer token changes and it is dynamic. Does anyone know how to automate this?
Postman has this nice feature of variables.
You can read more about them here:
https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables
You can store the bearer token in a variable and update it periodically as per your requirement.
You can modify this token in the pre-request script / tests script as per your needs.
For eg:
let token = pm.globals.get('dynamic_token');
token = 'newModifiedToken90332'; // Perform some operations here..
pm.globals.set('dynamic_token', token);
You can export the collection and globals into Newman and use them.
From Newman documentation you could do the following:
$ newman run mycollection.json -e dev_environment.json
-e is for using environment variables in which you can store your bearer token
Recall that you can export your collection and your environment variables as well.
Related
Keep getting 401 Unauthorized answer for requests in Newman.
I have a collection in Postman which I'm trying to execute in Newman. I exported it and tried to execute using this command:
newman run collection.json -g globals.json --insecure
Collection.json being the collection which I'm trying to run and globals.json containing the bearer token which I give to collection and is being inherited from folder brand for each request.
The same collection runs perfectly with this token in Postman but when I try in Newman it keeps giving 401.
What's the issue? Should I put directly the bearer token in each request and not use it separately as a global?
How can I obtain a Spotify access token for my own user, but from Postman ?
I want to use Postman to fetch the access token from Spotify (without a preceeding manual log in).
If that's complicated, I would accept to manually log in first,
before fetching the token from Postman.
Details:
To get an access token (to be used further in my own Postman requests),
I manually have to get one, while logged in on the Spotify Developer website.
Therefore, I would like to obtain it directy from Postman,
where I could immediately use it further in other requests/tests.
How could this be achieved ?
I did define an app on Spotify (so I have the client_id and client_secret).
I also have tried sending the cURL suggested in this Client Credentials Flow (one of the Authorization flows). Unsuccessfully:
curl -X "POST"
-H "Authorization: Basic ZjM4ZjAw...WY0MzE="
-d grant_type=client_credentials
https://accounts.spotify.com/api/token
Postman actually has all the various types of Spotify auth requests nicely packaged up in an exportable set of requests.
Came across it while researching the same issue and came across their blog post on using PKCE instead of implicit OAuth2 flows.
My coding skills and familiarity with Google Cloud solutions are limited and I'm trying to consume a Machine Learning model from a chatbot build using the platform Chatfuel.
I've trained a Natural Language Machine learning model using Google NL and I wanted to use this code snippet provided in my model page:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json" \
https://automl.googleapis.com/v1/projects/123456/locations/us-central1/models/ABC123:predict \
-d #request.json
I've then tried to pass this information in Chatbot like this:
The problem is it looks like I need to pass a token (API key seems not possible for this API) and I don't see how from the Chatfuel interface I could obtain a token for each request and pass it in my post request.
Edit: I've seen this post and created a service account but I don't see how to pass the credentials to Chatfuel.
How can I pass Google API service account credential to Chatfuel?
You should make a service account with Google Cloud.
It should give you a JSON with an API key, which is used to generate a new token every hour or so. Using the Google API for whatever server side language you are using (or just using the native http requests), you use the service key (which in general is loaded into your server as a .json file) to fetch a new token every hour; OR you can use Google Apps Script, if they have Chatfuel available (which I'm not sure if they do) and get the token there, and send it to your own app every 30 minutes or so via a trigger.
But the main thing is: using your service API key, you make an HTTP request to a certain api URL (available on with instructions from another page from that link above) every hour or so to generate a new token.
From the docs, that URL appears to be:
POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/SA-4#PROJECT-ID.iam.gserviceaccount.com:generateAccessToken
just replace, "`
SA-4#PROJECT-ID.iam.gserviceaccount.com
`"
with your own service account name
That's about it, its how it works for all Google Cloud APIs
As per google's docs, I'm generating my oauth access token like this:
export TOKEN=$(~/go/bin/oauth2l fetch -jwt -json ~/.google/my-service-key.json cloud-platform)
I'm then doing requests to Google's REST API like this:
curl -v -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d (...) $URL
The response I'm getting back from Google is that I'm not providing an OAuth token, when I clearly am:
Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
Other posts suggest to use gcloud auth application-default print-access-token instead of the OAuth token, but I know this to be the incorrect approach, as Google's API responds back that it wants a service account OAuth token and not an identity.
Any idea what's happening here?
Sometimes an old (bad) token gets cached from before you rotated the service_account.json.
Delete Cache
Try this:
rm ~/.oauth2l
Token vs JWT
And try getting an API token before you sign the JWT:
oauth2l fetch cloud-platform
Scope vs Audience
Also, the API token requires a scope (shown above), whereas the JWT requires an audience aud, which is a URL:
oauth2l fetch --jwt https://www.googleapis.com/auth/cloud-platform
ENVs
You may also want to make sure that you don't have competing configuration, see if GOOGLE_APPLICATION_CREDENTIALS is set.
echo $GOOGLE_APPLICATION_CREDENTIALS
unset GOOGLE_APPLICATION_CREDENTIALS
Or potentially use it instead of --json ./service_account.json:
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/.config/google/service_account.json
HTH
I have set up a collection in PostMan and am able to save my bearer token value to an environment variable successfully using the following test
var jsonData = JSON.parse(responseBody);
pm.environment.set("mytoken", jsonData.token);
but how do I set up a new call to use it?
I have tried adding a Header with
Authorization Bearer <mytoken>
but when I Post the Status is 401 Unauthorized
You can use Tests tab to write your code which updates the Environment variable, as explained in this link. Read more about Test scripts here.
Assuming the response of the auth call is:
{
"token": "woaejrlajfaoidhfalskdjfalsdijfasd"
}
Then, in Tests tab, you can write like:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.token);
This will update the variable token whenever you trigger the auth call. This token variable should be used in headers of all the API calls, to update automatically.
Do also check inheriting the auth.
In the headers I needed to use
for the key
Authorization
for the value
Bearer {{mytoken}}
Summary:
Create a variable to store Auth Token value in single place to use throughout your collection.
Set default method for Authorization for your entire collection.
Instead of setting the Authorization header for each request set the Authorization on each request to use "Inherit auth from parent" to automatically populate the request with the proper auth headers.
You can define variables in Postman environments and collections in order to simplify your requests by setting a value in one place and reference it in as many places as necessary. So you can create a variable for your Bearer Token value. Do this by editing your collection and going to the Variables tab to add a new variable.
Also while editing your collection go the Authorization tab to set a default authorization for all requests within your collection. You can set the Authorization Type for your collection to Bearer and set the Token value to be your defined variable. This will allow you to use the same authorization token for all of your requests within your collection:
Then in order to use the collection's default method of authorization, you will need to set the requests within that collection to set the Authorization Type to "Inherit auth from parent". Doing this will allow you to not have to deal with adding the Authorization header manually on to each request. Each request within the collection with the "Inherit auth from parent" authorization type selected will automatically populate the request with the proper headers for authorization if you have defined a default option for the collection like in the previous image.
Cheers!
I use a script after login post into tests tab like below;
let jsonData = JSON.parse(responseBody);
pm.collectionVariables.set("jwt_token", jsonData.data.token);
and create a collection variable like following;
Like the way Kristen, said. Or else download latest postman desktop application, in that in authorization they have an option to add bearer token in the header
pm.environment.set("JWT",pm.response.json().token)
Note : JWT is the environment variable you set in your environment