Postman pre-request to get token doesn't work - javascript

At this time I can perform succesfully my Postman request to get my token. I'm using these parameters :
-Basic Authorization in the headers
-and this body
-
Now I would like to get this request as a pre-request script (and use an environment variable for the token).
Here is the script :
pm.sendRequest({
url: 'http://localhost:8084/oauth/token',
method: 'POST',
header: {
'Authorization':'Basic Y2xpZW50OnBhc3N3b3Jk',
'content-type': 'application/x-www-form-urlencoded'
},
data:{
'password':'secret',
'username':'admin',
'grant_type':'password'
}
}, (err, res) => pm.environment.set("token", res.json().access_token));
It doesn't work with the response : Full authentication is required to access this resource.
What is wrong?
Thanks

You could change the data section to something like this?
body:{
mode:"urlencoded",
urlencoded:[
{
key:"grant_type",
value:"password"
},
{
key:"username",
value:"admin"
},
{
key:"password",
value:"secret"
}
]
}
A great resource for pm.sendRequest() examples can be found here

Related

How to Generate Google Drive File Copy in a Bulk

I want to create file copies in a bulk with one call, now I am using Google Drive Rest API for copying files.
Code:
copyFileCreation(_id, _fileId, _token) {
console.log({ _id }, { _fileId }, { _token });
fetch(`https://www.googleapis.com/drive/v2/files/${_fileId}/copy`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${_token}`,
'Accept': 'application/json'
},
body: JSON.stringify({
"parents": [{
"id": _id
}],
"title": "Untitle"
}),
}).then(res => res.json()).then(data => {
console.log('copyFileCreation_data', { data });
});
}
How can I achieve my goal?
Reference: https://developers.google.cn/drive/api/v2/reference/files/copy
Have you consulted the documentation? Did you read the part where it says
Files.copy is singular one file per call.
The Batching endpoint might be a solution but that depends upon what you qualify as one call.
Batching will let you nest multiple copy calls into a single HTTP Request, but if you are trying to get around the quota limitations this will not help you as the quota is based upon each of the calls with in the batch request.

Create a gist using GitHub API

I wanna create a gist using GitHub API. I tried an POST ajax request:
var gist = {
"description": "Avatars",
"public": true,
"files": {
"check.txt": {
"content": "Avatars list..."
}
}
};
$.ajax({
url: 'https://api.github.com/gists',
type: 'POST',
dataType: 'json',
data: JSON.stringify(gist),
success: function(e) {
console.log(e);
},
error: function(e) {
console.error("Error!: ", e);
}
});
But I always get the following error:
jquery-3.1.1.min.js:4 POST https://api.github.com/gists 401 (Unauthorized)
Can anyone help me?
Thanks
When you want to edit things on Github, you need to authorize your request. Either by adding a username and password to the request or an oauth token.
More information can be found in the authorization documentation: https://developer.github.com/v3/auth/
Since I ran across the same problem recently, let me add the examples requested by T.Todua
If you want to authenticate with username and password, add the following lines to your $.ajax request:
crossDomain: true,
beforeSend: function (XHR) {
XHR.setRequestHeader(
'Authorization','Basic ' + btoa(Username + ':' + Password)
);
},
If, however, you created an access token for your gists (see Github help and don't forget to check the "Gist" permission!) then add the following lines instead
crossDomain: true,
headers: {
'Authorization':'bearer ' + GitHubAccessToken
},
The GitHubAccessToken will be shown once (and only once!) immediately after creation, so make sure to store it in a safe location as everybody knowing this access token will be able to modify your gists (until you revoke it again).

Nodejs request post with body include api key

I have been trying about a week but I couldn't make a post request to get a result.
I tried a bunch of middlewares (exp: 'request', 'axios', 'reqclient','superagent etc..) but I couldn't make it.
Please provide me a simple post request with sending API key and body.
I also read all the documentation.
Please check below to see what I want :
*Authentication API key required.
*O-Auth Scopes trades
*Input One of: user_id + token or user_url is required.
here is my one of try :
const request = require('request-promise')
const options = {
method: 'POST',
uri: 'api-site.com/Offer/v1/',
headers: {
'User-Agent': 'Request-Promise',
'Authorization': 'Basic 123123asdasd123123'
},
body: {
user_url: "site.com/user/user1234123",
otherparams: "parameter"
},
json: true
};
request(options)
.then(function (response) {
Console.log(response);
})
.catch(function (err) {
console.log('Error ', err.message);
});
I am getting this output :
Error : 401 - {"status":401,"time":1540458426,"message":"API Key Required"}
I tried some other request post middle-wares and played with content-type (application/json. dataForm, x-www-form-urlencoded) or
changed the location of my API key from header to body or
tried my API key inside of auth{authorization: "API Key"}
tried much more.
the result didn't change. I got the same output or errors.
EDIT :
this is the link that I am trying to do but got stack :
check here
Solved !
Everything works great. Problem was I needed to send my API Key base64 string.
Buffer.from("your_api_key_value" + ":", "ascii").toString("base64")

How to send chat message using skype BOT api

I want to send messages,images ... using skype BOT API. If there is any other NPM available for doing skype chat.
Refer how to get access token from the docs
https://azure.microsoft.com/en-us/documentation/articles/active-directory-v2-protocols-oauth-code/
Then using this token i will try to send message using the following request
Refer docs:
https://docs.botframework.com/en-us/skype/chat/#navtitle
conversationId: 29:f2ca6a4a-93bd-434a-9ca5-5f81f8f9b455
request({
url: 'https://api.skype.com/v3/conversations/29:f2ca6a4a-93bd-434a-9ca5-5f81f8f9b455/activities',
method: "POST",
json: true,
headers: {
Authorization: ' Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IjIifQ.eyJpYXQiOjE0NzMzMTk4MDEsImV4cCI6MTQ3MzQwNjqwqwqwqwt5cGVpZCI6ImFtdF8wNjIiLCJzY3AiOjk1OCwiY3NpIjoiMCIsImNpZCI6ImVhODhhYWFmMmFkMjYwYzEiLCJhYXQiOjE0NzMzMTk4MDF9.ZrC0weALCz7QbUHFslJZD7L16k_ciFSCNY-q29h99x70qNrpB5e71KYrD18FTZ-3tI8Ck37_91yMHleQZvEziyEq5-t9EOaGM32RiF0iwnKZcbkOkvgqofWmcGdPT63HEyjWBHg3e_NLIE-RnDob4vMCQrHTkqmuQq6cVaIDkjke1Yi4xjONUNIB9QpWmpuRju0Kxi7oIJqiHWQK',
'Content-Type': 'application/json'
},
body: {
"type": "message/text",
"text": "Hi! (wave)"
}
}
But got error:
{ status: { code: 40499, text: 'No handler found for resource' } }
How to get conversationId?
You are addressing different domain. url should be https://apis.skype.com/v3....

simple $http.get request with params

I'm trying to make following http request using angularjs $http service
$http.get('http://myserver:8080/login?', {
params: {username: "John", password: "Doe" },
headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}).then(function(response) {
alert('success');
alert(response);
}, function(x) {
alert('err');
});
};
my server is up and runing and I'm getting following firebug console error
So what I'm doing wrong here in sending http request?
Here is an example for send GET request using $http.get with params
$http.get('api/anyApi/get', { params: {name:name}})
As you see, you do not need absolute path and ? param to use.
Also, as jfadich mentioned - do not use GET for such requests.

Categories