Search code examples
scalatwittertwitter-oauthscribe

How to post tweets using scribe?


I am having some problems working around with Scribe java library for Twitter. I succeeded in logging in twitter using the library and collecting the user profile data, but can't seem to figure out how to post tweets on behalf of the user.

I looked at the example from the github page but it was only for accessing data, not posting.

This is a method I came up with, after doing some research, what should I pass on the URL, if I was reading data then url="https://api.twitter.com/1.1/account/verify_credentials.json" would have been valid. But since I am trying to post a tweet. What should I do??

 def request(verb: Verb, url: String, accessToken: org.scribe.model.Token, reqBody: String, d: String): String = {
    val request: OAuthRequest = new OAuthRequest(verb, url)
    request.addBodyParameter("status", reqBody)
    service(d).signRequest(accessToken, request)
    val response: Response = request.send
    response.getBody
  }

The service method, for connection

  def service(url: String): OAuthService = new ServiceBuilder()
    .provider(classOf[TwitterApi.Authenticate])
    .apiKey("XXXXX")
    .apiSecret("XXXXX")
    .callback(url)
    .build

Can someone, provide me with some ideas and pointers.

Thank you,

@update: It seems, for posting status , url=https://api.twitter.com/1.1/statuses/update.json?status=helllllooooo is the required URL,thanks to this SO post , seems to do the trick, So,If for additional features:

  • How to post hashtags, Is appending #hash1 in the message body enough, or there are special way to do so??
  • And how do we post links, like any normal links, appends the links in message??
  • And how can we share links, with images??

I have been researching for the answers myself as well, hope you could provide me with some insights as well,


Solution

  • I think the pieces of documentation you need are the POST statuses/update page:

    https://dev.twitter.com/docs/api/1.1/post/statuses/update

    and this specific FAQ about link wrappers...

    https://dev.twitter.com/docs/tco-link-wrapper/faq#When_are_links_wrapped_with_t.co_Do_I_need_to_make_an_extra_API_call

    In summary, hashtags and URLs/shortening with look after themselves - you just need to make sure you URL encode as appropriate.

    You share a link with an image the same way you share any other URL (you just include it in the URL-encoded string that's POSTed). An image will show up if the underlying site is setup/the target URL is tagged to present a Twitter cards, as described here:

    https://dev.twitter.com/docs/cards