Request Comment on Behalf not working with zendesk API - node.js

I'm making a Node application that uses Zendesk API. Users logged in and can create tickets through my application.
To create tickets, I use this configuration
var config = {
method: 'post',
url: 'url/api/v2/requests.json',
headers: {
'Authorization': 'Bearer adminToken',
'X-On-Behalf-Of': 'emailOfUser',
'Content-Type': 'application/json',
},
data: ticket
};
axios(config)
.then(function (response) {
//My code
})
With X-On-Behalf-Of I can create tickets for the user logged in with my admin token.
However, it doesn't work for updating the ticket with comments. I got a Forbidden error. Here is my code :
var config = {
method: 'put',
url: 'url/api/v2/requests/' + idTicket,
headers: {
'Authorization': 'Bearer adminToken',
'X-On-Behalf-Of': 'emailOfUser',
'Content-Type': 'application/json',
},
data: { "request": { "comment": { "body": message, "public": true, "author_id": userId } } }
};
And when I remove the X-On-Behalf-Of, the comment is publish, but with my name, the admin name, and not the user name.
Do you know a solution for that ?
Thanks

It is likely that your token is missing impersonate scope.
Example for authorization code grant:
https://{subdomain}.zendesk.com/oauth/authorizations/new?client_id=foo&redirect_uri=bar&scope=impersonate+write&response_type=code

Related

Got npm module: How to add bearer token to POST request

I recently found out that request is no longer maintained, so the best alternative I found is got. I am trying to make an external API call to a REST Server but I am unsure as to how I can add a Bearer token in the authorization header of the POST request.
This is my code:
const response = await got.post(
"https://${SERVER}/${SOME_ID}/conversations/${CONVERSATION_ID}/messages",
{
json: [
{
text: req.body.message,
type: "SystemMessage",
}
],
responseType: "json",
headers: {
token: "Bearer pXw4BpO95OOsZiDQS7mQvOjs"
}
}
);
This results in a 401 Unauthorized. I was unable to find direction to such implementation in the documentation provided by GOT. And since there are not a lot of queries regarding this package, I was unsuccessful in finding anything on Google as well. If anyone can help me out in this regard, that would be very helpful!
Are you sure that the header name is "token" ?
Usually in API, the Bearer is in a header called "Authorization"
const response = await got.post(
"https://${SERVER}/${SOME_ID}/conversations/${CONVERSATION_ID}/messages",
{
json: [
{
text: req.body.message,
type: "SystemMessage",
}
],
responseType: "json",
headers: {
"Authorization": "Bearer pXw4BpO95OOsZiDQS7mQvOjs"
}
}
);
Here is the code
npm i postman-request link for npm package
const request = require('postman-request');
request({
url: 'your url',
headers: {
'Authorization': 'Bearer 71D50F9987529'
},
rejectUnauthorized: false
}, function(err, res) {
if(err) {
console.error(err);
} else {
console.log(res.body);
}
});

Discogs API : Can't get anything else than "401 You must authenticate to access this resource."

I did the Oauth flow like the docs says and got the oauth_token and the oauth_token_secret, then from my nodejs server I tried this request :
request.get({
headers: {
"User-Agent": "FooBarApp/3.0",
"Authorization": {
oauth_token:"my token",
oauth_token_secret: "my secret token",
"OAuth oauth_consumer_key":"mykey",
"oauth_nonce":Date.now(),
"oauth_signature":"mypass&",
"oauth_signature_method":"PLAINTEXT",
"oauth_timestamp":Date.now(),
"oauth_verifier":"users_verifier"
},
"Content-Type": "application/x-www-form-urlencoded"
},
url: "https://api.discogs.com/oauth/identity"
I also tried to remove all parameters in "authorization" except my two tokens but nohting work. Any clues ?
The documentation is wrong and Discogs is probably too lazy to update it. I've tried to send corrections for the docs to the technical team but it's a dead end.
Their authentication mechanism IS NOT OAuth 1.0 Revision A compliant even tho they advertise otherwise.
In the meantime, here are the headers you need to make an authenticated request:
const request = require('request');
const options = {
url: 'https://api.discogs.com/oauth/identity',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'OAuth oauth_consumer_key="YOUR_CONSUMER_KEY", oauth_nonce="' + Date.now() + '", oauth_token="OAUTH_TOKEN_RECEIVED_FROM_STEP_4", oauth_signature="YOUR_CONSUMER_SECRET&OAUTH_TOKEN_SECRET_RECEIVED_FROM_STEP_4", oauth_signature_method="PLAINTEXT", oauth_timestamp="' + Date.now() + '"',
'User-Agent': 'YOUR_USER_AGENT/1.0'
}
};
request(options, (err, res, body) => {
if (!err && res.statusCode == 200) {
console.log(JSON.parse(body));
}
});
Good luck !

In what format should a fetch POST request be created to access the Imagga API?

I am building a mobile application using React-Native that recommends clothing to users. I am using Imagga's API to get the colors of the clothing while excluding the background. I have tried to make a POST request using fetch from analyzing the node.js code given in the documentation:
image_file_b64 = "" + image_file_b64
//Extracting the colors from the object
let response = await fetch('https://api.imagga.com/v2/colors', {
method: 'POST',
headers: {
'apiKey': '<PLACEHOLDER>',
'apiSecret': '<PLACEHOLDER>',
'Authorization': '<PLACEHOLDER>',
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
image_base64: image_file_b64,
extract_overall_colors: 0,
})
})
let responseJson = await response.json()
console.log(responseJson)
However, the only output that I have received is (what is logged on the last line):
Object {
"status": Object {
"text": "Please provide content for processing.",
"type": "error",
},
}
I've worked with someone from Imagga to solve this issue, but he wasn't familiar with react native. he suggested changing the content-type to "application/x-www-form-urlencoded" or "application/x-www-form-urlencoded;charset=UTF-8", but neither have worked.
I am fairly confident that the problem is from the way that I set up my fetch. If anybody is familiar with the Imagga API, can you please identify what in the code is wrong or the mismatch in formatting between what Imagga expects and what I am giving it that results in it not thinking that I have given it input. Thanks!
the fetch body is not correct, the Content-Type that you use is JSON, why you send the string. modify it as the following, and try it.
let response = await fetch('https://api.imagga.com/v2/colors', {
method: 'POST',
headers: {
'apiKey': '<PLACEHOLDER>',
'apiSecret': '<PLACEHOLDER>',
'Authorization': '<PLACEHOLDER>',
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: {
image_base64: image_file_b64,
extract_overall_colors: 0,
})
})
I read the official API, it gives the node.js example. you can according to it and modify. If the above code is not successful, you can change the content-type to formdata
let params = {
image_base64: image_file_b64,
extract_overall_colors: 0,
};
let formData = new FromData()
formdata.append('RequestData',JSON.stringify(params))
let response = await fetch('https://api.imagga.com/v2/colors', {
method: 'POST',
headers: {
'apiKey': '<PLACEHOLDER>',
'apiSecret': '<PLACEHOLDER>',
'Authorization': '<PLACEHOLDER>',
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formData)
})
this is the official api, and you can use postman software to test the request

How to authenticate at MongoDB Atlas API using digest authentication?

I want to get a list of projects in MongoDB using its API "https://cloud.mongodb.com/api/atlas/v1.0/groups" but every time I get error as "401 You are not authorized for this resource".
According to the docs digest authentication is used.
Seems like I am passing the Private_key and Public_key in the wrong way.
Below is my request object
{
url: 'https://cloud.mongodb.com/api/atlas/v1.0/groups',
method: 'GET',
headers: {
'Accept': 'application/json',
},
auth: {
user: 'Public_Key',
pass: 'Private_key'
}
}
Can anyone please help me with this.
What you are missing is the key "sendImmediately". You need to send it in your auth object as follows :
request({
method: 'GET',
auth: {
"user": Public_Key,
"pass": Private_key,
"sendImmediately": false
},
url: 'https://cloud.mongodb.com/api/atlas/v1.0?pretty=true'
})

Mailchimp: The API key provided is linked to a different datacenter

I am trying to update a Mailchimp list but receive the following error:
{
"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title":"Wrong Datacenter",
"status":403,
"detail":"The API key provided is linked to a different datacenter",
"instance":""
}
However, the data-center referenced in my request URL is the same (us14) as the one suffixing my API key.
request.put({
url: 'https://us14.api.mailchimp.com/3.0/lists/xxxxxxxxx/members/',
auth: {
user: 'apikey:xxxxxxxxxxxxxxxxxxxxx-us14'
},
data: {
email_address: email,
status_if_new: 'subscribed',
email_type: 'html'
}
}
I have tried generating new API keys to no avail (they're all in us14).
Ok I was able to get this to work by first passing your API Key via the headers object. Second, I wrapped my data in JSON.stringify to ensure MailChimp was receiving a proper JSON Object on post. See below for sample code, hope this helps:
request.post({
url: 'https://usXX.api.mailchimp.com/3.0/lists/xxxxxxx/members',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic xxxxxxxxxxxxxxxxxxxxxxxxx-usXX'
},
form: JSON.stringify({
email_address: req.body.email,
status: 'subscribed',
interests: { 'xxxxxxx': true } // Interest Group
})
}, function(err, httpResponse, body) {
res.send(body);
});
const options = {
method: "POST",
auth: "uname:apikey656a******d2dfdb37c071a7cc-us19" //Should not give a space after a colon after uname
}
I had given a Space after the colon of uname. Now the API is working fine

Resources