Search code examples
iphoneiostwittertwrequest

How to attach geolocation to a tweet using TWRequest


I'd like to attach the location of the user to a TWRequest. I've tried to modify the url (with something like this: "http://api.twitter.com/1/statuses/update.json?lat=37.76893497&long=-122.42284884") but the response was "401 Unauthorized" or "400 Bad Request".

My TWRequest:

TWRequest *postRequest = [[TWRequest alloc] initWithURL:
                                               [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:[tweet text] forKey:@"status"] requestMethod:TWRequestMethodPOST];

Thanks in advance.


Solution

  • You attempt to perform request which requires authorization. Just initialize account property. Look at a good article about Twitter API: http://iosdevelopertips.com/core-services/ios-5-twitter-framework-part-2.html

    UPD: And you can't mix POST and GET variables, I mean you have to specify all parameters to NSDictionary (POST parameters), not to URL.

    NSURL *updateURL = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[tweet text], @"status",
        @"-122.42284884", @"long",
        @"37.76893497", @"lat", nil];
    TWRequest *postRequest = [[TWRequest alloc] initWithURL:updateURL                  
                                                 parameters:dict
                                              requestMethod:TWRequestMethodPOST];
    

    UPD2: If geo-location isn't work, check if location is enabled for twitter account (go to Twitter site - Settings - Account - Tweet Location). Look at the REST API documentation on Twitter site update request section:

    About geo

    Any geo-tagging parameters in the update will be ignored if geo_enabled for the user is false (this is the default setting for all users unless the user has enabled geolocation in their settings)