0

I am writing an app in iOS that integrates to Instagram. I am getting an Http Error 400 from Instagram when using the Like API call. The other API calls are working fine however. Here is part of my code where I post the request

{
    NSDictionary* params = [NSDictionary dictionaryWithObject:_accessToken forKey:@"access_token"];
    NSString* path = [NSString stringWithFormat:kUserLikeEndPoint, medID];

    NSURL *url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:kInstagramBaseURLString]];

    NSMutableArray *mutableParameterComponents = [NSMutableArray array];
    for (NSString *key in [params allKeys]) {
        NSString *component = [NSString stringWithFormat:@"%@=%@", [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[params valueForKey:key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [mutableParameterComponents addObject:component];
    }


    url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", [mutableParameterComponents componentsJoinedByString:@"&"]]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setAllHTTPHeaderFields:defaultHeaders];

    likeInstagramConnection = [NSURLConnection connectionWithRequest:request delegate:self];

}

It is during this connection that I get the following Error

Error Domain=HTTP Code=400 "HTTP Error" UserInfo=0xd9571b0 {NSLocalizedDescription=HTTP Error} desc <NSHTTPURLResponse: 0xd9574a0> { URL: https://api.instagram.com/v1/media/632580883841234624_257450972/likes?access_token=ACCESS_TOKEN_GOES_HERE } {
    status code: 400, headers {
        Connection = "keep-alive";
        "Content-Language" = en;
        "Content-Length" = 92;
        "Content-Type" = "application/json; charset=utf-8";
        Date = "Tue, 28 Jan 2014 07:17:52 GMT";
        Server = nginx;
        "Set-Cookie" = "csrftoken=5eaa89da550811f9d62d5c15525ceebf; expires=Tue, 27-Jan-2015 07:17:52 GMT; Max-Age=31449600; Path=/, ccode=US; Path=/";
        Vary = "Cookie, Accept-Language";
        "X-Ratelimit-Limit" = 5000;
        "X-Ratelimit-Remaining" = 4997;
    }
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 1
    Get a network analyzer like Charles Proxy (30 day fee trial). It is easy to use and supports https connections. – zaph Jan 29 '14 at 00:31
  • If I enter the same http request in Instagram API console, it works fine. – user2827665 Jan 29 '14 at 17:08
  • You have a bug in your code. You will have to figure out what it is. One way is to look at what you are "on-the-wire" and what is being returned. When I work on this kind of problem I have Charles running and the console open to see any errors. You can setup Charles to display only calls to a specific URL, Instagram in the case. – zaph Jan 29 '14 at 18:00
  • Hi Zaph, When I saw your post it looked like an ad. I'll try the Charles Proxy. – user2827665 Jan 29 '14 at 18:03
  • Zaph, The request looks like complete jiberish – user2827665 Jan 29 '14 at 18:12
  • ”œRÈC€Êv[√µJ µæ)Qìјplí∑Ù\rÍMpœ }∏¶≠ß&˛Â˝á·i©m°‡æ2(¶Ñjp|‚ø]∆Jˇ¿$¿#¿ ¿ ¿¿¿(¿'¿¿¿¿¿&¿%¿*¿)¿¿¿¿¿¿¿¿ =5 gk39WìãóÿÆàıÚ‰ıb{πı ˙iúä¯Á⁄$qæ%Dpä쟑®‘º∞B‰¥ï"kí0pÉ<äØÛÄ{Œq¶„NCú@ı lÃ[FBkZ¡IûÅ®¶ ˝êÃ3&·G1¶Í ú(ı∫$Ö1{£CÉ5˘≤v‚˙©·2O—h#∏>é 00íU“Ï‹*2èÙGÍIòQ4Rà÷’ª˚ëIôfxqWeÌ’ë≈Ò@Œ”Èõ˜ê˛Ñò^ ƒˆrfi" ›pK∞ – user2827665 Jan 29 '14 at 18:12
  • If the connection is https you will have to install the dummy certificate and add the URL to Proxy : Proxy Settings : SSL. https encrypts the connection, Charles can, via being a proxy, display the plain text. – zaph Jan 29 '14 at 19:05
  • I am on a Mac. I downloaded the Charles Proxy, added it to the Key Chain. I closed and restarted Safari and Firefox. I then updated the Proxy Settings in Charles, Add the api.instagram.com:443 and Left everything else untouched. When I re-execute the code I get 2 entries in Charles under api.instagram.com 1) Connected to remote Host and 2) SSLHandshake Failed, remote host closed connection. Did I miss a step? – user2827665 Jan 29 '14 at 22:59
  • After turning off the MacOS proxy I was able to connect but now Charles proxy doesn't show the request or the response. It is completely blank. – user2827665 Jan 30 '14 at 06:23
  • Charles Proxy is not helping with my original problem. Why am I getting Error 400. – user2827665 Jan 31 '14 at 17:48

1 Answers1

0

Might be an expired access token. I know in python I am getting this error code in an API call exception, and I can't see why they would make status codes differ between languages. Hope that gets you in the right direction.

adambullmer
  • 952
  • 9
  • 29