0

I want to use bit.ly to track my itunes affiliate links. I get affiliate links from http://target.georiot.com. It works when oppening the direct link (going to itunes). But when i shorten the affiliate link with bitly, it doesn't go on the same page.

Here is the code for getting the shorten url:

NSString *longURL = link;
NSString *bitlyRequestURLString = [NSString stringWithFormat:@"http://api.bit.ly/shorten?version=2.0.1&format=xml&login=%@&apiKey=%@&longUrl=%@",
                                   @"myappname",
                                   @"myappidentifier",
                                   longURL];
NSURL *bitlyURL = [NSURL URLWithString:bitlyRequestURLString];

// get the short URL from bit.ly
NSError *error;
NSString *response = [NSString stringWithContentsOfURL:bitlyURL encoding:NSUTF8StringEncoding error:&error];

NSString *shortURL = @"";
NSArray *responseParts = [response componentsSeparatedByString:@"<shortUrl>"];

if ([responseParts count] > 1) {
    NSString *responsePart = [responseParts objectAtIndex:1];
    responseParts = [responsePart componentsSeparatedByString:@"</shortUrl>"];

    if ([responseParts count] > 0) {
        shortURL = [responseParts objectAtIndex:0];
    }
}

Last redirect link goes someting like "http://phobos.apple.com/WebObjects/...."

Any Ideas? Thanks

user1078065
  • 412
  • 1
  • 5
  • 19

2 Answers2

0

You probably need to URL encode your longURL before sending it in the query string to bit.ly

You can use the NSString method stringByAddingPercentEscapesUsingEncoding:

NSString *longURL = [link stringByAddingPercentEscapesUsingEncoding:
 NSASCIIStringEncoding];
NSString *bitlyRequestURLString = [NSString stringWithFormat:@"http://api.bit.ly/shorten?version=2.0.1&format=xml&login=%@&apiKey=%@&longUrl=%@",
                                   @"myappname",
                                   @"myappidentifier",
                                   longURL];
jonkroll
  • 15,682
  • 4
  • 50
  • 43
  • You have a point here. But for example : ...url=http%3A%2F%2Fitunes.apple.com%2Fbr%2Fapp... goes into: ...url=http%253A%252F%252Fitunes.apple.com%252Fbr%252Fapp... Does bit.ly know how to handle this? Asking because i tried it's the same problem occuring – user1078065 Feb 22 '12 at 21:46
  • `NSString *encodedURL = (NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef) longURL, NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);` this solved the problem – user1078065 Feb 22 '12 at 21:54
0

I just tried using the bit.ly REST API to create a short url and the URL returned works as expected, see below. It looks like the prior answer suggesting encoding was on target, and standard url encoding (percent encoding, such as http://meyerweb.com/eric/tools/dencoder/) seems to do the trick.

This call (with proper API key): https://api-ssl.bitly.com/v3/shorten?login=georiot&apiKey=R_MY_API_KEY_HERE&longUrl=http%3A%2F%2Ftarget.georiot.com%2FProxy.ashx%3Fgrid%3D64%26id%3D8i%2FET44NjHw%26offerid%3D146261%26type%3D3%26subid%3D0%26tmpid%3D1826%26RD_PARM1%3Dhttp%3A%2F%2Fitunes.apple.com%2Fus%2Falbum%2Fmetallica%2Fid278116714%3Fuo%3D4%26partnerId%3D30%2F&format=json

Returned: { "status_code": 200, "status_txt": "OK", "data": { "long_url": "http://target.georiot.com/Proxy.ashx?grid=64&id=8i/ET44NjHw&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=http://itunes.apple.com/us/album/metallica/id278116714?uo=4&partnerId=30/", "url": "http://bit.ly/zR6uzb", "hash": "zR6uzb", "global_hash": "wFpgG2", "new_hash": 1 } }

The result url works as expected (after removing the escape /'s): http:\bit.ly\zR6uzb

At GeoRiot, we have also recently added a new integrated url shortener which might be of interest to you, however we haven't exposed an API for it quite yet. If you are interested in giving this a shot when we have it available, please let us know. The big benefit here is that the extra redirect between bit.ly and georiot would be removed, speeding up the response time for your users quite a bit.

Anyway, its been a while since the original post, so hopefully you got this figured out. If not let us know and we'll help where we can!

Geniuslink
  • 722
  • 3
  • 5