Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Works, Images Not So Much #33

Closed
gregphillips03 opened this issue Jul 19, 2018 · 2 comments
Closed

Text Works, Images Not So Much #33

gregphillips03 opened this issue Jul 19, 2018 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@gregphillips03
Copy link

gregphillips03 commented Jul 19, 2018

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?

@dandv dandv added the help wanted Extra attention is needed label Jan 5, 2019
@dandv dandv pinned this issue Jan 5, 2019
@dandv
Copy link
Collaborator

dandv commented Jan 5, 2019

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 dandv closed this as completed Jan 5, 2019
@dandv dandv unpinned this issue Jan 5, 2019
@gregphillips03
Copy link
Author

gregphillips03 commented Jan 15, 2019

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Development

No branches or pull requests

2 participants