Hi there!
Can you please help us to understand why we receive error status 401 code 32 whenever we initialize POST direct_messages/events/new (message_create). Thanks.
1 Like
Have you set your permissions correctly? To use direct messaging endpoints you must set permissions for direct messages on the developer portal
2 Likes
Same error here!
- Iâm correctly getting the OAuth token (with user context)
- I set permissions to use direct messaging endpoints (see pic)
- Iâm generating a correct OAuth 1.1 header signature (I tested the algorithm to generate the signed header with other APIs that donât require user context, and it worksâŚ)
Any idea on why we get this error?
was the access token reset after changing the permissions? (Access tokens keep their permissions)
1 Like
Yes @IgorBrigadir, I regenerated the tokens multiple times after updating the permissions
1 Like
Do you have more than one application? If so, make sure the application credentials are not confused with other applications.
Sometimes this small issue is missed from checking when we are working on some apps
Thanks, @gauloics. I have only one application!
With the same OAuth tokens I can post tweets on behalf of the user, but when it comes to sending a direct message I get 401 code 32âŚ
1 Like
Just to make sure, Is the message youâre trying to attach a media or text message to?
@gauloics just a text message
I tried and direct message endpoint no problem.
maybe you can give an example of the code you are using along with the error code
Thanks, @gauloics!
Maybe @DevelopmentZq can provide the snippet of code to facilitate the debug here?
(Iâm using their plugin/library to get the token with user context)
Hey there!
We did exactly as suggested, but no luck, still getting a 401 error. We use the âOAuth-1.0aâ library to generate tokens, and ârequestâ to send the request.
Here is the Snippet:
function(properties, context) {
const request = require(ârequestâ);
const OAuth = require(âoauth-1.0aâ);
const crypto = require(âcryptoâ);
const consumer_public = context.keys.ConsumerPublic;
const consumer_secret = context.keys.ConsumerSecret;
// Initialize
const oauth = OAuth({
consumer: {
key: consumer_public,
secret: consumer_secret,
},
signature_method: âHMAC-SHA1â,
hash_function(base_string, key) {
return crypto
.createHmac(âsha1â, key)
.update(base_string)
.digest(âbase64â)
},
})
let text = âTest messageâ;
let recipientID = â1196817277234089985â;
const dataString = {"event": {"type": "message_create", "message_create": {"target": { "recipient_id": "${recipientID}"},"message_data": {"text": "${text}"}}}};
const request_data = {
url: âhttps ://api.twitter.com/1.1/direct_messages/events/new.jsonâ,
method: âPOSTâ,
data: dataString
}
const token = {
key: properties.oauth,
secret: properties.oauth_secret,
}
let oauthData = oauth.authorize(request_data, token);
return context.async(function (cb) {
request(
{
url: request_data.url,
method: request_data.method,
headers: oauth.toHeader(oauthData),
json: request_data.data
},
(error, response, body) => {
// const cd = response.statusCode == 200;
const resobj = JSON.stringify(response);
if (!error) {
cb(undefined, {
âresobjâ: resobj
})
} else {
cb(undefined, {
âerrorâ: error
})
}
}
)
})
}
Here are our settings:
1 Like
iâm not sure if this is the case, but first step try to use a library other than request, since the request has been deprecated
Reference: request
1 Like
This should be
key: "yourAccessToken",
secret: "yourAccessTokenSecret"
if you use token as another user(Not a developer account) you have to implement 3-legged OAuth flow To get access token and access token secret
CMIIWâŚ
1 Like
The request library didnât affect sending the request, unfortunately. What helped is the right parameters included in the header. Anyway thanks for the collaboration.
Have a great day!
ZQ Dev
2 Likes
when i copy paste this uri contains spaces after https, double check your code
1 Like
ah yes @DevelopmentZq, that space could be the problem actually thanks @gauloics
1 Like
Hey, @gauloics!
Yes, we know this, and we specifically created this space to send a message through the forum. The fact is that your forum does not allow you to send messages with links 
In our plugin, we obviously donât make this space.
Returning to our problem, we provided the correct parameters in the header and the private messages were successfully sent, but this only happens with the old account created a year ago. The new accounts that were currently created cannot even be authorized. We donât get the Authorization Token.
Can you please help to figure out whatâs going wrong? Thanks.
1 Like