Text Works, Images Not So Much
gregphillips03 opened this issue · 2 comments
I have the following sitting in the AWS cloud API Gateway:
import Twitter from "twitter-lite";
import { success, failure } from "./libs/response-lib";
export async function main(event, context, callback) {
const data = JSON.parse(event.body);
const client = new Twitter({
subdomain: "api",
consumer_key: "redacted"
consumer_secret: "redacted"
access_token_key: "redacted"
access_token_secret: "redacted"
});
if(data.base64encodedimg)
{
try
{
client.subdomain = "upload";
const url = await client.post("media/upload", null, {
media_data: data.base64encodedimg,
});
client.subdomain ="api";
await client.post("statuses/update", null, {
status: data.tweet,
media_ids: url.media_id_string,
});
} catch (e) {
console.log(e);
callback(null, failure({ status: false }));
}
}
else
{
try
{
await client.post("statuses/update", null, {
status: data.tweet,
});
callback(null, success(data.tweet));
} catch (e) {
console.log(e);
callback(null, failure({ status: false }));
}
}
}
I can fire off tweets all day long with a problem, but I'm not able to attach an image to them. Tweeting without an image works without a hitch as well.
I've gone through the painful process of ensuring that I'm actually sending along a base64 encoded image, so I know that's coming through as expected, at least to the gateway.
When this fails, all I receive back is a 500 response.
Thoughts?
Seems like a duplicate of #29 - can you look into setting the subdomain to upload
instead of api
?
We do need better documentation for the subdomains, CC @peterpme
@dandv
Thanks for your response. I actually tried that in the past (realized the above example I posted contained an immutable)
I can fire off text tweets, but still images fail.
`import Twitter from "twitter-lite";
import { success, failure } from "./libs/response-lib";
export async function main(event, context, callback) {
const data = JSON.parse(event.body);
const client = new Twitter({
subdomain: "api",
consumer_key: "REDACTED",
consumer_secret: "REDACTED",
access_token_key: "REDACTED",
access_token_secret: "REDACTED"
});
const upload_client = new Twitter({
subdomain: "upload",
consumer_key: "REDACTED",
consumer_secret: "REDACTED",
access_token_key: "REDACTED",
access_token_secret: "REDACTED"
});
if(data.img)
{
try
{
const url = await upload_client.post("media/upload", null, {
media_data: data.img,
});
const resp = await client.post("statuses/update", null, {
status: data.tweet,
media_ids: url.media_id_string,
});
callback(null, success(resp.id_str));
} catch (e) {
console.log(e);
callback(null, failure({ status: e}));
}
}
else
{
try
{
const resp = await client.post("statuses/update", null, {
status: data.tweet,
/* lat: 37.7821120598956,
long: -122.400612831116,
display_coordinates: true,*/
});
callback(null, success(resp.id_str));
} catch (e) {
console.log(e);
callback(null, failure({ status: false }));
}
}
}`
The try block catches this:
{name: FetchError,
type: invalid-json,
message: invalid json response body at https://upload.twitter.com/1.1/media/upload.json?media_data=(all of my b64 encoded image) reason: Unexpected end of JSON input}
But I'm not sure where that comes from; i.e., Twitter or my server.
At any rate, it looks like a valid JSON object isn't getting sent? Possibly when the payload is bundled and sent off? I'm lost on this one.