How To: Update your Twitter status with ASP

Here is a simple script I wrote in classic ASP to update your Twitter status via the Twitter API. This script handles basic HTTP authentication to validate your Twitter account and URL Encoding to send over friendly status updates.

<%
Response.Buffer = True
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLHTTP")

twitter_username = "username"	'change to your twitter username
twitter_password = "password"	'change to your twitter password

new_status = "visit strangework.com!"		'change to your new status

xml.Open "POST", "http://" & twitter_username & ":" & twitter_password & "@twitter.com/statuses/update.xml?status=" & server.URLencode(new_status), False
xml.setRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1"
xml.Send

Response.Write xml.responseText		'view Twitter's response

Set xml = Nothing
%>

Popularity: 32% [?]

Related posts:

  1. How to: Update FriendFeed using ASP
  2. How to: Update Pownce using ASP
  3. How To: Create ASP and AJAX username availability check example
  4. How To: Install WordPress Plugin Twitter Tools
  5. 301 Permanent Redirect using ASP

Tags: , , , ,

38 Comments so far »

  1. Nicolai RyghNo Gravatar said

    am January 5 2008 @ 7:55 am

    Hi Brad!

    Thanks! I’ll include it in my posting tool for my blog!

  2. Paul MiddletonNo Gravatar said

    am October 23 2008 @ 12:29 am

    Wow – this worked first time…THANK YOU, THANK YOU, THANK YOU!

  3. Twitter API integration « WebEquity.org Weblog said

    am October 23 2008 @ 1:17 am

    [...] (some projects are just like that, right?), I came across exactly what I was looking for – a Twitter API integration script for classic ASP. Thanks, [...]

  4. AllenNo Gravatar said

    am December 5 2008 @ 2:19 pm

    Why would I get an error on the .Open?

    error ‘80004005′

    /include/updateProfile.asp, line 24

  5. BradNo Gravatar said

    am December 11 2008 @ 8:14 pm

    Make sure you have the XMLHTTP component installed correctly.

  6. Allen McGuireNo Gravatar said

    am December 11 2008 @ 8:25 pm

    I’m at the mercy of a shared hosting plan, so I took a different approach:

    Dim xmlhttp
    Set xmlhttp = Server.CreateObject(“MSXML2.ServerXMLHTTP”)

    xmlhttp.open “POST”, “http://twitter.com/statuses/update.xml?source=madtownlounge&status=” & value, False,oRs(“MemberTwitterLogin”),oRs(“MemberTwitterPassword”)
    xmlhttp.setRequestHeader “Authorization”, “Basic ” & Base64Encode(oRs(“MemberTwitterLogin”) & “:” & oRs(“MemberTwitterPassword”))
    xmlhttp.send

  7. JustinNo Gravatar said

    am January 20 2009 @ 6:58 pm

    Fantastic Brad, thanks for posting this – opens a world of possibilities:D

  8. Eric LeVineNo Gravatar said

    am January 27 2009 @ 1:15 pm

    FYI, you may think you are doing a POST, but you are actually still submitting things on the querystring with your syntax above. To get it into the POST object you need to submit the querystring parameters as an argument to the send method. Also, the open method takes optional username, password parameters, so it is nicer to send them that way versus as part of the URL, otherwise you run into URL encoding woes if a user has a :, %, / or @ in their password.

    Kudos to these guys: http://www.4guysfromrolla.com/webtech/110100-1.2.shtml

    Try this code instead:

    Response.Buffer = True
    Dim xml
    Set xml = Server.CreateObject(“Microsoft.XMLHTTP”)

    twitter_username = “username” ‘change to your twitter username
    twitter_password = “password” ‘change to your twitter password

    new_status = “visit strangework.com!” ‘change to your new status

    xml.Open “POST”, “http://twitter.com/statuses/update.xml”, False, twitter_username, twitter_password
    xml.setRequestHeader “Content-Type”, “application/x-www-form-urlencoded”
    xml.Send “status=” & server.URLencode(new_status)”

    Response.Write xml.responseText ‘view Twitter’s response

    Set xml = Nothing

  9. Eric LeVineNo Gravatar said

    am January 27 2009 @ 1:17 pm

    Whoops, delete the extra ” in my example above:

    Response.Buffer = True
    Dim xml
    Set xml = Server.CreateObject(”Microsoft.XMLHTTP”)

    twitter_username = “username” ‘change to your twitter username
    twitter_password = “password” ‘change to your twitter password

    new_status = “visit strangework.com!” ‘change to your new status

    xml.Open “POST”, “http://twitter.com/statuses/update.xml”, False, twitter_username, twitter_password
    xml.setRequestHeader “Content-Type”, “application/x-www-form-urlencoded”
    xml.Send “status=” & server.URLencode(new_status)

    Response.Write xml.responseText ‘view Twitter’s response

    Set xml = Nothing

  10. AstralisNo Gravatar said

    am February 14 2009 @ 12:53 pm

    Thank you! Are there other Classic ASP code that you can share to interact with Twitter such as auto-reply to those who follow you?

  11. ZubairNo Gravatar said

    am March 5 2009 @ 6:32 am

    Hi this works a treat… however it breaks if you try to login using your email address…. any ideas how come?

    thx

  12. Khris WoodringNo Gravatar said

    am March 13 2009 @ 8:56 pm

    wow. i rarely come across well written scripts that work the first time. you rock my man!

  13. Chris J. PoppNo Gravatar said

    am March 23 2009 @ 9:49 pm

    Tried your code and it’s nice.

    I run a radio station and want to tweet what’s playing when a song changes.

    I’ve only been able to send these new tweets when the page I have the code on is refreshed in my browser.

    I’m looking to have something where the tweets will be updated when a file is generated with the message in it. In this case, the message is a song title and artist.

    I’ve been able to semi-automate this by throwing a meta refresh tag into my .asp page and having the page opened in a browser. There’s gotta be a way to streamline this…

  14. Allen McGuireNo Gravatar said

    am March 23 2009 @ 9:54 pm

    You should ask WJJO 94.1 – they are a Madison-based radio station and they are posting the same thing you are looking for.

    http://twitter.com/WJJO

  15. AstralisNo Gravatar said

    am March 23 2009 @ 11:56 pm

    When the file is generated via ASP, run this code on the next line after the file is generated. It’s that simple.

  16. IainNo Gravatar said

    am April 11 2009 @ 6:52 am

    Hi there,

    Great code and worked first time.

    What’s the deal with specifying the source of the status update.

    Adding:

    source=XXX

    into the post data works only if Twitter recognises the XXX, othertimes it doesn’t. Do you have to register the source somewhere?

    Thanks.

  17. BradNo Gravatar said

    am April 11 2009 @ 6:24 pm

    You have to email Twitter and ask to be added to the source list. I contacted Biz Stone and he added SnapFoo.com to the Twitter source, so anything coming from SnapFoo said so on Twitter.

  18. gummylickNo Gravatar said

    am April 14 2009 @ 10:22 am

    perfect! thanks!

  19. BenjaminNo Gravatar said

    am April 21 2009 @ 4:04 am

    fantastic little piece of code, great job!

  20. Soda BobNo Gravatar said

    am April 22 2009 @ 10:31 am

    Outstanding, worked without a hitch. Probably could have figured this out eventually, but this saved me a lot of experimentation time. Thanks!

  21. Soda BobNo Gravatar said

    am April 22 2009 @ 10:42 am

    “Hi this works a treat… however it breaks if you try to login using your email address…. any ideas how come?”

    Ever hear the story about the man who went to see his doctor and told him, “doctor, my arm hurts when I raise it above my head”? The doctor replied, “don’t raise it above your head.”
    :)

    To answer your question, though, twitter uses a standard built in web server login method, which requires a username and password, and can’t equate your email with that username. So, just use the username instead of the email. If you’re working with a bunch of different users using the same script (or whatever), you could create either an array or a database with username/email address pairs, then use that array or database to match a given email address with the username, then use that matched username in the script on this page.

  22. AsdrubalNo Gravatar said

    am May 9 2009 @ 4:34 pm

    Hi,

    The code works perfectly but.. I have a problem and I hope you may help me.

    When I try to send a message like this:
    Reverón: titiritero
    Twitter show me this:
    Reverótitiritero

    And I really give up trying to know why?

    Please give me a hand on this!

    Thanks!

  23. Steve OwenNo Gravatar said

    am May 14 2009 @ 12:43 pm

    A lot of people (myself included) seem to have permission issues using Microsoft.XMLHTTP to access a remote site. Even the fixes for relaxing security didn’t help me. So I just used Softwing.AspTear, which is a free dll. The equivalent code is:

    Set URLFetchObj = CreateObject(“SOFTWING.AspTear”)
    On Error Resume Next
    sResponse = URLFetchObj.Retrieve(“http://twitter.com/statuses/update.xml”, 1, “status=” & Server.URLencode(“[tweet text]“), “[twitter_username]“, “[twitter_password]“)
    If Err.Number 0 Then sResponse = Err.Number & ” ” & Err.Description
    On Error Goto 0

    Response.Write “Twitter says: ” & sResponse

  24. Richie RichNo Gravatar said

    am June 6 2009 @ 10:16 am

    Anyone know how to implement this code using Classic ASP and OAuth?

    (as it seems the oauth is now a requirement with Twitter)

  25. RickNo Gravatar said

    am July 12 2009 @ 11:39 am

    Couldn’t get this to work. Kept getting “could not authenticate you” errors. Yes, I made sure my username and password were correct. Anyone else having this problem?

  26. RubisNo Gravatar said

    am July 20 2009 @ 6:23 pm

    Same problem as Asdrubal concerning accentuation marks and punctuaction.
    Any solutions?

  27. Kenny BruntonNo Gravatar said

    am August 5 2009 @ 4:56 am

    Excellent example folks, was most impressed.

    Richie Rich, don’t think it’s a requirement yet, but I too would also be interested in seeing any other examples.

  28. Thiago SantosNo Gravatar said

    am August 5 2009 @ 3:43 pm

    Excellent!!!
    Very easy to use.

  29. ArtNo Gravatar said

    am August 7 2009 @ 3:34 pm

    I just created a new Twitter account today and tried the code. I don’t get any error messages, but only a blank screen when I call my function. Anyone know if it takes time for this to work after creating an account?

  30. Richie RichNo Gravatar said

    am August 11 2009 @ 8:23 am

    @Kenny BruntonNo any joy with a Classic ASP version of twitter/oauth? Getting desperate now :)

  31. AndreNo Gravatar said

    am September 7 2009 @ 1:23 am

    Hi

    Anyone got an idea if I can get the person Icon with Classic ASP?

    Best regards

    Andre
    antispam@key.co.za

  32. RONo Gravatar said

    am September 8 2009 @ 4:50 pm

    Thanks for the code! Works the 1st time.

    Andre, if you parse the xml response, it’s in the profile_image_url tag.

  33. AndreNo Gravatar said

    am September 8 2009 @ 10:06 pm

    Thanks RO. Yes, I have coded a parser using INSTR and MID, but is there a cleaner way to read that tag using asp. Eg GetElementsByTagName(“image”)

  34. SanxNo Gravatar said

    am September 16 2009 @ 7:17 pm

    Andre,

    Use: objXMLdoc.GetElementsByTagName(“user/profile_image_url”).Item(0).text

    That will give you the URL of the user’s profile image.

  35. Coolguy97No Gravatar said

    am October 28 2009 @ 2:49 am

    What about Posting a tweet using the OAuth.
    Using asp.net.

  36. AndreNo Gravatar said

    am November 24 2009 @ 11:40 am

    Hi

    I’m creating a project page to get a whole lot of people together to create an oauth library for Twitter using classic asp.

    The code group is at http://code.google.com/p/twitter-oauth-in-classic-asp/

    and the groups at

    http://groups.google.com/group/twitter-oauth-in-classic-asp

    Please join and help!

  37. jeremy flintNo Gravatar said

    am December 4 2009 @ 11:34 am

    I am trying to append a hashtag to the status, but the API seems to ignore it. Has anyone had any luck with that?

    I am trying it several different ways. Including it as a hidden field value and building the status using Request.Form, by adding it to the end of the URL that is being generated as & “+%23tagname”.

    Any ideas?

  38. k gNo Gravatar said

    am January 22 2010 @ 2:52 am

    is there anyway in classic asp to have some one login or signup via their twitter account if your site is backed by an access database, like twitpic users are able to signup for that site via twitter and began using it.. i havent found any twitter login info that coincides with classic asp

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: