Instagram Getting user details - ios

Am trying to integrate instagram in my app,while getting user details after getting access token,its showing error . Am appending data in NSData and trying to print it in connectionDidFinishLoading(NSURLConnection *)connection ( NSURLConnection Delegate) . While converting NSData to id ,its showing null .Also if i am converting NSdata to NSString am getting Response as
{"meta":{"error_type":"OAuthAccessTokenException","code":400,"error_message":"The access_token provided is invalid."}}{"meta":{"code":200},"data":{"username":"XXXX","bio":"","website":"","profile_picture":"http:sampleiamge.jpg","full_name":"samplename","counts":{"media":1,"followed_by":0,"follows":0},"id":"sampleID"}}
If am printing the json as id jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
getting error as
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be
completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0xa967bc0
{NSDebugDescription=Garbage at end.}
Tried all jsonreading option ,but no result
Any hep will be appreciable.
Thanks

You could also have a look at the InstagramKit which does this fairly straight forward. https://github.com/shyambhat/InstagramKit
There's a demo app so you can see how the authentication is done and request a user's details in just a few lines of code. You don't really need to mess around with the access token data format.

It seems like you are not authenticate.
There is a project in GitHub that implements a simple UIViewController that performs the Instagram auth process step by step:
https://github.com/Buza/Instagram-Auth-iOS
You can see all process from request token to error management.
Hope it helps!

The response occurs when the access_token is invalid, try printing (NSLog) out the URL your are making the request and make sure the access_token is correctly passed.

Related

AFNetworking -1017 error

I am working on iOS application and facing one strange issue. I am using AFNetworking framework to communicate with server (HTTPS communication). I am retrieving student data from server by using "getStudentData" web service API. It is post request. It works for all user ids except one. It fails when we have a data for more than 450 students. Below are the error details,
Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo=0x7bf9c7d0 {NSErrorFailingURLStringKey=https://www.fdmobileservices.com/mAccountsWeb/services/speedpass/rpc, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=https://www.fdmobileservices.com/mAccountsWeb/services/speedpass/rpc, NSLocalizedDescription=cannot parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x7bf9a380 "cannot parse response"}
As per error description, It tells "Could Not parse", so I think it may be due to server is returning "nil" or some data other than JSON format.
But I am not able to trace it since from application side it directly goes into below method,
- (void)connection:(NSURLConnection __unused *)connection
didFailWithError:(NSError *)error
Is there any way from application side to trace root cause? This method works for other user login except one.
I tried using web client to access this service by same user login, it works well and return data of 450 students. I think due to some reason iOS network layer rejects this. I am trying to find out.
Thanks in advance.
As you can see here, kCFURLErrorCannotParseResponse = -1017. So probably this is saying that there is something wrong in the parameters.
If You're using AFNetworking 2.0, I suggest You to deep debug your response flow with severals breakpoints.
Specifically try to set a breakpoint on AFURLResponseSerialization.m at the start of the method:
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error;
Inside the method, if the below condition is true, probably there is something wrong (but you can step into validateResponse:data:error: to better understand what's wrong):
if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error])
If the above condition is false you can then check about the generated responseString:
NSString *responseString = [[NSString alloc] initWithData:data encoding:stringEncoding];
If you're expecting json data, check that responseString is not nil and data data is valid, maybe also the stringEncoding.
Also ensure that the contentType that you're receiving is the one you're expecting.

Error in JSON Serialisation with HTML Content

I have a strange issue while getting response from SVC webservice.
I have a response as below:
When I am fetching content from "AboutOrganization" key.
I am getting error as below:-
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Something looked like a 'null' but wasn't around character 324840.) UserInfo=0xc52345b0 {NSDebugDescription=Something looked like a 'null' but wasn't around character 324840.}
I have used NSJSONSerialization to get data from Webservice response.
I am not sure, But getting this type of Error because JSON object contents HTML escape sequence characters as below:
If I am right then give me solution for the same. And If i am wrong then guide me why i am getting this type of error.
Appreciate your time and help
Thank you in advance.

initWithContentsOfURL returns nil only in iOS 5.x [duplicate]

I have been stuck with this for a while and don't seem to get around this.
I am trying to read the contents of an URL as a string from an URL, But i get a weird
Error -> Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"
My code :
fetchedString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"www.example.com/iphone"] encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#",fetchedString);
// if there is something wrong with the URL
if (error) {
NSLog(#"Error -> %#", error);
return ;
}
What am I doing wrong? I tried using getting as NSData as well, but I get null back.
Yes, the URL is missing the scheme: "http://".
"Error -> Error Domain=NSCocoaErrorDomain Code=256"
For the error code check the Apple documentation:
NSError codes in the Cocoa error domain.
NSFileReadUnknownError = 256,
NSFileReadUnknownError
"Read error, reason unknown"
Not that the error definition is very helpful. :-)
Also do not check if error is nil to determine if there is an error, check the return value for nil. error is not guaranteed to be nil on successful execution.
I had a similar problem accessing files located on my device. I followed NSURL isFileURL always returns NO
and used [NSURL fileURLWithPath] instead of [NSURL URLWithString] - this worked!
I got this error (Error Domain=NSCocoaErrorDomain Code=256) as soon as our ssl certificate expired. that may not help you but could help someone else.
Sandboxing
If you're using sandboxing in your app, you might want to check that com.apple.security.network.client is set to YES. It's in the the General tab of your Target in Xcode 5 under
Network: Outgoing Connections (Client)
Also be aware that if you see a code 257 when trying to reach a file:/// url, that's also probably because of sandboxing, but this time rather the File Access part. Because I didn't want to open it to anything else than `com.apple.security.files.user-selected.read-write'
User selected files
I preferred to use A Dead Simple Fileserver and use http://localhost:3000 when in Debug mode.
More reasons that might be causing this specific error:
SSL is misconfigured on the server
The server redirects (301) the http URL to https (see #1)
App transport security also uses this code for blocked requests.
I got the same error. The above marked answer is perfect. But in my case, I had the "http://" in the url but had to add the port number in the url request since there is a service running on a specific port that is actually responding to your request.
#"http://example.com:8084/yyy.zzz"
I got the same error, the above solution didn't work for me, in my case i was calling dataWithContentsOfURL from within a UNNotificationServiceExtension so i had to update the info.plist file of the UNNotificationServiceExtension with the app transport security entries.

NSURLConnection sendSynchronousRequest response is nil upon invalid credential

I'm getting a weird behavior where using sendSynchronousRequest or sendAsynchronousRequest with invalid credential will make the nsurlresponse nil. But the old way with the [[NSURLConnection alloc] initWithRequest:request delegate:self]; I get the 401 response code.
The error value using the sendSynchronousRequest or sendAsynchronousRequest is
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be
completed. (NSURLErrorDomain error -1012.)" UserInfo=0x756ecb0
{NSErrorFailingURLKey=myurl,
NSErrorFailingURLStringKey=myurl,
NSUnderlyingError=0x75704d0 "The operation couldn’t be completed.
(kCFErrorDomainCFNetwork error -1012.)"
Does someone have a clue as to why it is like that? Any information on this is appreciated. I was expecting to get a 401 response from sendSynchronousRequest or sendAsynchronousRequest
Thanks,
This error is caused when a WWW-Authenticate: HTTP header is returned with the 401 response, asking for user-interaction to enter valid credentials. The NSURLConnection processes the HTTP headers and the response body, returning the response data correctly as an NSData object, but leaving the returningResponse object as nil.
According to Apple's Foundation Constants Reference, the error -1012 is:
NSURLErrorUserCancelledAuthentication
Returned when an asynchronous request for authentication is cancelled by the user.
This is typically incurred by clicking a “Cancel” button in a username/password dialog, rather than the user making an attempt to
authenticate.
I am speculating that it automatically treats the WWW-Authenticate: header as a request for credentials that was cancelled by the user (since it is headless) and generates the error. Then some other part of the code-path that should set the response is not executed because there was an error. Personally I think this is either a bug or bad design. It should be ok to get a valid HTTP response and an error at the same time, either way, you get a valid HTTP response so the returningResponse should be set. Bad Apple!

NSJSONSerialization gives me error "Duplicate key"

I request a JSON response from a home controlling device:
NSData* responseData = [NSData dataWithContentsOfURL:url];
The JSON data in responseData is complete and valid. However, if I try
NSDictionary* dict = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSLog("Error: %#", error);
it gives me the following error:
Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Duplicate key for object around character 0.) UserInfo=0x6a8b8e0 {NSDebugDescription=Duplicate key for object around character 0.}
I searched the web but couldn't find anything helpful so far.
Edit: The JSON response it quite long, so instead of posting it here, please find it here.
It looks like the JSON is fine by pasting it into http://jsonformat.com/ - maybe you found an Apple bug? Another way to try and validate would be to use a third-party iOS library and see if it gives you the same error? Say, TouchJSON?
I get the same error, only at random when the app runs. When the NSJSONSerialization fails I've checked the json object it chocks on using a JSON Viewer. The JSON is always OK. Bugs in NSJSONSerialization when it's called several times?
I got the same error. My JSON data passed JSON validator at http://jsonformatter.curiousconcept.com/ . However, I found two duplicated keys in my JSON (at the end of dictionary). After I removed those keys, NSJSONSerialization works smoothly.

Resources