androidtwitter

How do you find a user's Twitter id from their username in Android


In my android app, I want users to be able to enter a twitter username and from there the twitter app launches on the entered username's page. I did some research and found out the link needed to open the twitter app from another app is twitter://user?user_id=id_num I was wondering if there is a way to get the user's twitter id from a twitter username in Android so I can make this happen. Any help would be greatly appreciated, thank you.


Solution

  • You should read through the Twitter API, in particular the section about user lookups
    Edit: Links updated to current Twitter API docs though answer remains targeted at API v1.1 as when asked.

    That particular request has optional fields to search by id or screen_name and will return a list of matches which you can parse for user ids. So you can search by screen_name and read the response to get an id.

    The request you are (were) looking for is:

    GET https://api.twitter.com/1/users/lookup.json?screen_name=somename
    

    The provided response in the API examples is (was):

    [
      {
        "name": "Twitter API",
        "profile_sidebar_border_color": "87bc44",
        "profile_background_tile": false,
        "profile_sidebar_fill_color": "e0ff92",
        "location": "San Francisco, CA",
        "profile_image_url": "http://a3.twimg.com/profile_images/689684365/api_normal.png",
        "created_at": "Wed May 23 06:01:13 +0000 2007",
        "profile_link_color": "0000ff",
        "favourites_count": 2,
        "url": "http://apiwiki.twitter.com",
        "contributors_enabled": true,
        "utc_offset": -28800,
        "id": 6253282,
        "profile_use_background_image": true,
        "profile_text_color": "000000",
        "protected": false,
        "followers_count": 160752,
        "lang": "en",
        "verified": true,
        "profile_background_color": "c1dfee",
        "geo_enabled": true,
        "notifications": false,
        "description": "The Real Twitter API. I tweet about API changes, service issues and appily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
        "time_zone": "Pacific Time (US & Canada)",
        "friends_count": 19,
        "statuses_count": 1858,
        "profile_background_image_url": "http://a3.twimg.com/profile_background_images/59931895/twitterapi-background-new.png",
        "status": {
          "coordinates": null,
          "favorited": false,
          "created_at": "Tue Jun 22 16:53:28 +0000 2010",
          "truncated": false,
          "text": "@Demonicpagan possible some part of your signature generation is incorrect & fails for real reasons.. follow up on the list if you suspect",
          "contributors": null,
          "id": 16783999399,
          "geo": null,
          "in_reply_to_user_id": 6339722,
          "place": null,
          "source": "<a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>",
          "in_reply_to_screen_name": "Demonicpagan",
          "in_reply_to_status_id": 16781827477
        },
        "screen_name": "twitterapi",
        "following": false
      },
      {
        "name": "Twitter",
        "profile_sidebar_border_color": "EEEEEE",
        "profile_background_tile": false,
        "profile_sidebar_fill_color": "F6F6F6",
        "location": "San Francisco, CA",
        "profile_image_url": "http://a1.twimg.com/profile_images/878669694/twitter_bird_normal.jpg",
        "created_at": "Tue Feb 20 14:35:54 +0000 2007",
        "profile_link_color": "038543",
        "favourites_count": 2,
        "url": "http://twitter.com",
        "contributors_enabled": true,
        "utc_offset": -28800,
        "id": 783214,
        "profile_use_background_image": true,
        "profile_text_color": "333333",
        "protected": false,
        "followers_count": 3305606,
        "lang": "en",
        "verified": true,
        "profile_background_color": "ACDED6",
        "geo_enabled": true,
        "notifications": false,
        "description": "Always wondering what's happening. ",
        "time_zone": "Pacific Time (US & Canada)",
        "friends_count": 257,
        "statuses_count": 774,
        "profile_background_image_url": "http://s.twimg.com/a/1276896641/images/themes/theme18/bg.gif",
        "status": {
          "coordinates": null,
          "favorited": false,
          "created_at": "Tue Jun 22 16:40:19 +0000 2010",
          "truncated": false,
          "text": "9 cool things to do with your Twitter account (via @pastemagazine) http://example.com",
          "contributors": [
            16739704
          ],
          "id": 16783169544,
          "geo": null,
          "in_reply_to_user_id": null,
          "place": null,
          "source": "web",
          "in_reply_to_screen_name": null,
          "in_reply_to_status_id": null
        },
        "screen_name": "twitter",
        "following": false
      }
    ]
    

    So you can access the id from the results by parsing that JSON response for the "id" field.

    Note however that the returned is a JSON array of results and not just a single definitive answer. You will need to work out yourself which is the correct one (order is not guaranteed either so do not assume the first entry is the most likely).

    Also worth noting is that even though this is documented as returning 'id' and an integer value, the current preferred usage of ids is to supply 'id_str' and a string representation of the integer. This is due to inconsistencies in how various platforms handle and limit integers. So even if you receive 'id' you should use 'id_str' for future interactions.