Hello Stackoverfllow,
The context,
> nodejs: v16.17.0
> NPM: 8.15.0
> OS: Ubuntu 20.04.5 LTS
> Axiom:0.27.2
I Developing an Application using Twitter API. I use Account Activity API to get a webhook event from it. Then I will have a media endpoint from which I can download an image.
The most important thing is that I'm trying to /GET da images from the Twitter Media API. The response ( from Twitter ) gives me the image encoded under res.data
data: '����\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00��\x00C\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06\x07\x07\x06\b\n' +
'\x10\n' +
'\n' +
'\t\t\n' +
"\x14\x0E\x0F\f\x10\x17\x14\x18\x18\x17\x14\x16\x16\x1A\x1D%\x1F\x1A\x1B#\x1C\x16\x16 , #&')*)\x19\x1F-0-(0%()(��\x00C\x01\x07\x07\x07\n" +
'\b\n' +
'\x13\n' +
'\n' +
'\x13(\x1A\x16\x1A((((((((((((((((((((((((((((((((((((((((((((((((((��\x00\x11\b\x06@\x03�\x03\x01"\x00\x02\x11\x01\x03\x11\x01��\x00\x1B\x00\x00\x02\x03\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07��\x00\x19\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05��\x00\f\x03\x01\x00\x02\x10\x03\x10\x00\x00\x01�x\x7Fe��\x00������P\x14\x00�\x00\x02�%\n' +
'Q�_K:�*�]�R�%!�(�I�%\x19\x12@�RI�/O\x06�l4@BMP\f\x06@\x0
(I dont know what type of encoding ( I received ) and their doc does not mention it ). its been days now, and I had try to encode with Binary, base64 and UTC-8, which failed in all these attempted
I visited a lot of tutorials, but I could not see any solution that worked for me. , will link a few 1 , 2 , 3
Moreover, I could see the image from the postman client, but I didn't find any way to download this image as a jpg in the local drive.
Below is my snippet for the request
const nconf = require('nconf')
nconf.file({ file: 'config.json' }).env()
const axios = require('axios');
const crypto = require('crypto');
const oauth1a = require('oauth-1.0a');
const CONSUMERKEY = nconf.get('TWITTER_CONSUMER_KEY');
const CONSUMERSECRET = nconf.get('TWITTER_CONSUMER_SECRET');
const TOKENKEY = nconf.get('TWITTER_ACCESS_TOKEN');
const TOKENSECRET = nconf.get('TWITTER_ACCESS_TOKEN_SECRET');
function getAuthHeaderForRequest(request) {
const oauth = oauth1a({
consumer: { key: CONSUMERKEY, secret: CONSUMERSECRET },
signature_method: 'HMAC-SHA1',
hash_function(base_string, key) {
return crypto
.createHmac('sha1', key)
.update(base_string)
.digest('base64')
},
})
const authorization = oauth.authorize(request, {
key: TOKENKEY,
secret: TOKENSECRET,
});
return oauth.toHeader(authorization);
}
const request = {
url: 'https://ton.twitter.com/1.1/ton/data/dm/1564581847908098052/1564581845014126592/56XDEw1t.jpg',
method: 'GET',
};
const authHeader = getAuthHeaderForRequest(request);
/**
* Media in direct messages must be retrieved via an authenticated app-user GET request.
* It is advised that applications store user's access tokens to use for direct message media retrieval.
*
* @param {string} url - the url for the media i wanna retrive
* @return {Promise}
*/
function retrieveMediaApi (url){
// message_request_options.url = 'https://ton.twitter.com/1.1/ton/data/dm/1564581847908098052/1564581845014126592/56XDEw1t.jpg' //url.toString(); //should be the media url
return axios.get(
request.url,
{ headers: authHeader,
AcceptEncoding: 'gzip, deflate, br' });
}
module.exports= retrieveMediaApi;
Here how I try to save the date to the local drive
retrieveMediaApi(x.message_create.message_data.attachment.media.media_url_https).then(async newUrl => {
//newevent.message.payload[0].url = newUrl;
//await decode(newUrl, { fname: 'example', ext: 'jpg' });
console.log(newUrl);
console.log('start')
require('fs').writeFile('outout.jpg',newUrl.data, // to use with writeFile
{ encoding: "binary" }, // to use with writeFile ***************WORKING
(err) => {
if (err) {
console.log("An error ocurred while writing the media file.");
return console.log(err);
}
}
);
console.log('!')
}).catch(x=>{
logger(' ~ file: convert_formate.js ~ line 56 ~ retrieveMediaApi ~ x ', x);