How to upload image and status to twitter using twitter4j

瘦欲@ 提交于 2019-12-13 09:04:46

问题


I want to upload an image as well as status through my android application.

So far I have tried many examples, but none of them helped. (I already have the CONSUMER_KEY,CONSUMER_SECRET,TWITPIC_API_KEY and I'm able to post the status from my application,I only need the code for uploading an image).

How can I do that?


回答1:


You need to use ImageUpload class in twitter4j. The below code describes a typical scenario for image upload with text.

    AccessToken accessToken = twitterSession.getAccessToken();
    ConfigurationBuilder conf = new ConfigurationBuilder();
    conf.setOAuthConsumerKey(twitter_consumer_key);
    conf.setOAuthConsumerSecret(twitter_secret_key);
    conf.setUseSSL(true);
    conf.setHttpReadTimeout(2400000);
    conf.setHttpStreamingReadTimeout(2400000);
    conf.setOAuthAccessToken(accessToken.getToken());
    conf.setOAuthAccessTokenSecret(accessToken.getTokenSecret());
    conf.setMediaProviderAPIKey(twitpic_api_key);
    Configuration configuration = conf.build();
    OAuthAuthorization auth = new OAuthAuthorization(configuration);
    ImageUpload uploader = new ImageUploadFactory(configuration)
    .getInstance(auth);

    File photo=new File("abc/myimage.png");
    String status="Checkout my new image";

    uploader.upload(photo,status);



回答2:


At my time, the following sample is precious

https://github.com/learnNcode/DemoTwitterImagePost



来源:https://stackoverflow.com/questions/24692147/how-to-upload-image-and-status-to-twitter-using-twitter4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!