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

4

2 回答 2

0

在将查询字符串中的 longURL 发送到 bit.ly 之前,您可能需要对 longURL 进行 URL 编码

您可以使用该NSString方法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];
于 2012-02-22T21:36:24.810 回答
0

我刚刚尝试使用 bit.ly REST API 创建一个短 url,返回的 URL 按预期工作,见下文。看起来先前的答案表明编码是目标,标准 url 编码(百分比编码,例如http://meyerweb.com/eric/tools/dencoder/)似乎可以解决问题。

此调用(使用正确的 API 密钥): 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 %3D30%2F&格式=json

返回:{“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", "哈希”:“zR6uzb”,“global_hash”:“wFpgG2”,“new_hash”:1 } }

结果 url 按预期工作(删除转义 / 后):http:\bit.ly\zR6uzb

在 GeoRiot,我们最近还添加了一个新的集成 URL 缩短器,您可能会感兴趣,但是我们还没有公开它的 API。如果您有兴趣在我们有空时试一试,请告诉我们。这里最大的好处是 bit.ly 和 georiot 之间的额外重定向将被删除,从而大大加快了用户的响应时间。

无论如何,距离最初的帖子已经有一段时间了,所以希望你能明白这一点。如果不让我们知道,我们会尽我们所能提供帮助!

于 2012-03-04T02:51:13.930 回答