Error while doing FQLRequest - ios

I am doing a FQL request when in my view controller through a specific method.
Here is my request.
NSString* fql = [[NSString alloc] init];
fql = [NSString stringWithFormat:#"SELECT uid, name FROM user WHERE uid IN(SELECT uid FROM event_member WHERE eid= %#) AND sex='female'", event_id];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:fql forKey:#"query"];
[[Facebook shared] requestWithMethodName:#"fql.query" andParams:params andHttpMethod:#"POST" andDelegate: self];
[fql release];
However, when I just launch my request a second time, I got that error when using NSZombieEnabled :
-[CFString release]: message sent to deallocated instance 0x4cb9ec0
The thread points at [_params release] in FBRequest.m
When trying just changing the request with
fql = #"SELECT uid, name, sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex = 'male'";
I have no longer the error.
Here are the fbrequest delegate methods
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(#"FBRequestDelegate Received response");
}
- (void)request:(FBRequest *)request didLoad:(id)result {
NSLog(#"FBRequestDidLoad Received response");
self.allListProfils = result;
NSLog(#"all listprofil :%#", allListProfils);
[self resetSearch];
[DSBezelActivityView removeViewAnimated:YES];
moreButton.hidden = NO;
[table reloadData];
};
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(#"FBRequestDidFailWithError Received response, Error is : %#",[error description]);
};
Does it ring a bell for some of you ?
Thanks :)

Related

iOS Authenticate Azure Active Directory & get calendar events from office 365 exchange

Trying to Authenticate with Azure Active Directory and fetch mail, calendar data, accessToken is returned successfully:
authority = #"https://login.windows.net/common/oauth2/authorize";
redirectUriString = #"http://xxxxxx.xxxxxxx.com/oauth";
resourceId = #"https://outlook.office365.com";
clientId = #"xxxxxxx-xxxxx-xxx";
-(void) getToken : (BOOL) clearCache completionHandler:(void (^) (NSString*))completionBlock;
{
ADAuthenticationError *error;
authContext = [ADAuthenticationContext authenticationContextWithAuthority:authority
error:&error];
[authContext setValidateAuthority:YES];
NSURL *redirectUri = [NSURL URLWithString:redirectUriString];
if(clearCache){
[authContext.tokenCacheStore removeAllWithError:&error];
if (error) {
NSLog(#"Error: %#", error);
}
}
[authContext acquireTokenWithResource:resourceId
clientId:clientId
redirectUri:redirectUri
completionBlock:^(ADAuthenticationResult *result) {
if (AD_SUCCEEDED != result.status){
// display error on the screen
[self showError:result.error.errorDetails];
}
else{
completionBlock(result.accessToken);
}
}];
}
-(NSArray*)getEventsList
{
__block NSMutableArray * todoList;
[self getToken:YES completionHandler:^(NSString* accessToken){
NSURL *todoRestApiURL = [[NSURL alloc]initWithString:#"https://outlook.office365.com/api/v1.0/me/folders/inbox/messages?$top=2"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:todoRestApiURL];
NSString *authHeader = [NSString stringWithFormat:#"Bearer %#", #""];
[request addValue:authHeader forHTTPHeaderField:#"Authorization"];
[request addValue:#"application/json; odata.metadata=none" forHTTPHeaderField:#"accept"];
[request addValue:#"fbbadfe-9211-1234-9654-fe435986a1d6" forHTTPHeaderField:#"client-request-id"];
[request addValue:#"Presence-Propelics/1.0" forHTTPHeaderField:#"User-Agent"];
//[request addValue:#"true" forHTTPHeaderField:#"return-client-request-id"];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error == nil){
NSArray *scenarios = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
todoList = [[NSMutableArray alloc]initWithArray:scenarios];
//each object is a key value pair
NSDictionary *keyVauePairs;
for(int i =0; i < todoList.count; i++)
{
keyVauePairs = [todoList objectAtIndex:i];
NSLog(#"%#", keyVauePairs);
}
}
NSLog(#"Finished");
//[delegate updateTodoList:TodoList];
}];
}];
return nil; }
Error is returned in response object:
{
error = {
code = ErrorAccessDenied;
message = "Access is denied. Check credentials and try again.";
};
}
I know its late to answer this but it might be helpful for someone like me who was struggling to get the same thing done
I have done this using the office 365 SDK for iOS which has all the inbuilt classes to do your work.
If you download their sample code it will provide you all the details you require to do certain operations (mail, calendar, contacts, one drive).
Before using the SDK make sure you login to Azure AD and register your application and add permissions so that you do not get 403 error code or any access denied message.
I am using the below code to fetch my events details from outlook calendar
[self getClientEvents:^(MSOutlookClient *client) {
NSURLSessionDataTask *task = [[[client getMe] getEvents] read:^(NSArray<MSOutlookEvent> *events, MSODataException *error) {
if (error==nil) {
if (events.count!=0) {
dispatch_async(dispatch_get_main_queue(), ^{
for(MSOutlookEvent *calendarEvent in events){
NSLog(#"name = %#",calendarEvent.Subject);
}
});
}else{
NSLog(#"No events found for today");
}
}
}];
[task resume];
}];
getClientEvents is a method which gives call to the Office 365 SDK and fetches the event details of the user but it first fetches the token for the resource and then makes the call with the acquired token
-(void)getClientEvents : (void (^) (MSOutlookClient* ))callback{
[self getTokenWith : #"https://outlook.office365.com" :true completionHandler:^(NSString *token) {
MSODataDefaultDependencyResolver* resolver = [MSODataDefaultDependencyResolver alloc];
MSODataOAuthCredentials* credentials = [MSODataOAuthCredentials alloc];
[credentials addToken:token];
MSODataCredentialsImpl* credentialsImpl = [MSODataCredentialsImpl alloc];
[credentialsImpl setCredentials:credentials];
[resolver setCredentialsFactory:credentialsImpl];
[[resolver getLogger] log:#"Going to call client API" :(MSODataLogLevel *)INFO];
callback([[MSOutlookClient alloc] initWithUrl:#"https://outlook.office365.com/api/v1.0" dependencyResolver:resolver]);
}];
}
getTokenWith method fetches the token for a resource first and then with the acquired token makes the necessary calls to fetch the events, but before fetching the token it checks in the cache to see if there are any tokens available for the same resource.
// fetch tokens for resources
- (void) getTokenWith :(NSString *)resourceId : (BOOL) clearCache completionHandler:(void (^) (NSString *))completionBlock;
{
// first check if the token for the resource is present or not
if([self getCacheToken : resourceId completionHandler:completionBlock]) return;
ADAuthenticationError *error;
authContext = [ADAuthenticationContext authenticationContextWithAuthority:[[NSUserDefaults standardUserDefaults] objectForKey:#"authority"] error:&error];
NSURL *redirectUri = [NSURL URLWithString:#"YOUR_REDIRECT_URI"];
[authContext acquireTokenWithResource:resourceId
clientId:[[NSUserDefaults standardUserDefaults] objectForKey:#"clientID"]
redirectUri:redirectUri
completionBlock:^(ADAuthenticationResult *result) {
if (AD_SUCCEEDED != result.status){
[self showError:result.error.errorDetails];
}
else{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:result.tokenCacheStoreItem.userInformation.userId forKey:#"LogInUser"];
[userDefaults synchronize];
completionBlock(result.accessToken);
}
}];
}
getCacheToken method: Checks if there are any reusable token for any resources.
-(BOOL)getCacheToken : (NSString *)resourceId completionHandler:(void (^) (NSString *))completionBlock {
ADAuthenticationError * error;
id<ADTokenCacheStoring> cache = [ADAuthenticationSettings sharedInstance].defaultTokenCacheStore;
NSArray *array = [cache allItemsWithError:&error];
if([array count] == 0) return false;
ADTokenCacheStoreItem *cacheItem;
for (ADTokenCacheStoreItem *item in array) {
if([item.resource isEqualToString:resourceId]){
cacheItem = item;
break;
}
}
ADUserInformation *user = cacheItem.userInformation;
if(user == nil) return false;
if([cacheItem isExpired]){
return [self refreshToken:resourceId completionHandler:completionBlock];
}
else
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:user.userId forKey:#"LogInUser"];
[userDefaults synchronize];
completionBlock(cacheItem.accessToken);
return true;
}
}
Using this code and Office 365 SDK in place you can get the outlook events for a particular user, before that make sure you have full permissions in the Azure AD else you may get 0 events as response.
Please note all the methods are from the SDK example apart from the first method to view how to fetch the events i would recommend to download the exchange example from the github.
You can also use MSGraph SDK to fetch calendars and events:
Check this link: Configuration process is same, only fetching events is different(see given code for fetching events):
How to Fetch/Create calender by O365-iOS-Connect?
Note: Above link is used to fetch calendars from outlook the process is same for this but you should use this code after authentication and completed get events action look like this:
- (IBAction)getCalendarsEvents:(id)sender {
[NXOAuth2AuthenticationProvider setClientId:clientId
scopes:#[#"https://graph.microsoft.com/Files.ReadWrite",
#"https://graph.microsoft.com/Calendars.ReadWrite"]];
[[NXOAuth2AuthenticationProvider sharedAuthProvider] loginWithViewController:nil completion:^(NSError *error) {
if (!error) {
[MSGraphClient setAuthenticationProvider:[NXOAuth2AuthenticationProvider sharedAuthProvider]];
self.client = [MSGraphClient client];
// Authentication done
[[[[_client me] events] request] getWithCompletion:^(MSCollection *response, MSGraphUserEventsCollectionRequest *nextRequest, NSError *error){
NSArray *arr = response.value;
MSGraphEvent *event = arr.firstObject;
// Here you will getting outlook events
}];
}
}];
}

Parsing result URL from Facebook request API properly

I have written the following code which sends a predefined GraphAPI object to another Facebook user in my iOS app. This works well.
-(void)FacebookSendObjectWithIdentifier: (NSString *)objectIdentifier withObjectName:(NSString *)objectName withMessage:(NSString *)message withTitle:(NSString *)title withActionType:(NSString *)actionType runOnComplete:(void (^)(NSDictionary* request))completionBlock{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
// Optional parameter for sending request directly to user
// with UID. If not specified, the MFS will be invoked
//#"RECIPIENT_USER_ID", #"to",
// Give the action object request information
actionType, #"action_type",
objectIdentifier, #"object_id",
nil];
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:message
title:title
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Error launching the dialog or sending the request.
NSLog(#"Error sending %# request.", objectName);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User clicked the "x" icon
NSLog(#"User canceled %# request.", objectName);
} else {
// Handle the send request callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"request"]) {
// User clicked the Cancel button
NSLog(#"User canceled %# request.", objectName);
} else {
// User clicked the Send button
NSString *requestID = [urlParams valueForKey:#"request"];
NSLog(#"%# request sent, request id: %#", objectName, requestID);
if (completionBlock)
completionBlock(urlParams);
}
}
}
}];
}
[self parseURLParams:[resultURL query]]; is implemented throughout several Facebook documentation pages as follows:
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:#"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:#"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}
On return, the completion block parses the returned urlParams and saves them to our database to reference the object sent via Facebook. However, this should return a "request" parameter which holds the request ID, and an array, "to" which holds the Facebook IDs of the people the object was sent to. Instead, this returns two fields in the url, "request" which acts fine, and "to[0]" instead of "to" as an NSString instead of NSArray, since the method provided above parses the url parameters as strings rather than corresponding objects, which turns the array "to" into a "to[0]" NSString.
I am looking for either a better solution to parse the returned response URL from Facebook or a better way to send an object through Facebook's API. Preferablly the first option as this is the only step missing for the implementation to be complete.
Thank you.

iOS facebook integration - sending and receiving requests

I use a facebook api to connect to facebook and send request via native dialogs provided by the api.
I followed the sample posted in the docs on developers.facebook.com
But I have following problem sending requests :
1. The requests are not shown in notifications - only in application center - In this case i think that it is a problem of that the app is in sandbox and not posted to APPSTORE
I succesfully send request to facebook server with right fbUser id. But when I want to receive the notification in app here comes the problem :
Following the docs as an authorized user I should se
this in open url method:
fb[APP_ID]://authorize#expires_in=[ACCESS_TOKEN_EXPIRATION]
&access_token=[USER_ACCESS_TOKEN]
&target_url=https://apps.facebook.com/[APP_NAME_SPACE]/?request_ids=
[COMMA_SEPARATED_REQUESTIDs]&ref=notif&app_request_type=user_to_user
But i can see only plain login without targer url .... I can see session expiration date, fb app id, access token and so on. But no target url?
So basically what the target_url is?
How it should be set?
What i have to include when sending request?
In addition :
application handle open url method is called properly.
checkRequests method is also called properly after app becomes active.
Please do not link me to the docs. I have read it moreless 50 times and didn't find any reasonable solution...
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
self.openedURL = url;
NSLog(#"%#",url);
return [FBSession.activeSession handleOpenURL:url];
}
- (void)sendRequest {
FBSBJSON *jsonWriter = [FBSBJSON new];
NSDictionary *gift = [NSDictionary dictionaryWithObjectsAndKeys:
#"5", #"points",
#"1", #"badge",
nil];
NSString *giftStr = [jsonWriter stringWithObject:gift];
NSMutableDictionary* params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Hi from test app", #"message",
giftStr, #"data",
nil];
[self.facebook dialog:#"apprequests"
andParams:params
andDelegate:self];
}
// Handle the request call back
- (void)dialogCompleteWithUrl:(NSURL *)url {
NSDictionary *params = [self parseURLParams:[url query]];
NSString *requestID = [params valueForKey:#"request"];
NSLog(#"Request ID: %#", requestID);
}
-(FBSession*)returnSession{
return self.session;
}
/*
* Helper function to get the request data
*/
- (void) notificationGet:(NSString *)requestid {
[FBRequestConnection startWithGraphPath:requestid
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
NSString *title;
NSString *message;
if ([result objectForKey:#"data"]) {
title = [NSString
stringWithFormat:#"%# sent you a gift",
[[result objectForKey:#"from"]
objectForKey:#"name"]];
FBSBJSON *jsonParser = [FBSBJSON new];
NSDictionary *requestData =
[jsonParser
objectWithString:[result objectForKey:#"data"]];
message =
[NSString stringWithFormat:#"Badge: %#, Karma: %#",
[requestData objectForKey:#"badge"],
[requestData objectForKey:#"points"]];
} else {
title = [NSString
stringWithFormat:#"%# sent you a request",
[[result objectForKey:#"from"] objectForKey:#"name"]];
message = [NSString stringWithString:
[result objectForKey:#"message"]];
}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil,
nil];
[alert show];
// Delete the request notification
[self notificationClear:[result objectForKey:#"id"]];
}
}];
}
/*
* Helper function to check incoming URL
*/
- (void) checkIncomingNotification {
if (self.openedURL) {
NSString *query = [self.openedURL fragment];
if (!query) {
query = [self.openedURL query];
}
NSDictionary *params = [self parseURLParams:query];
for (NSString * str in [params allKeys]) {
NSLog(#"key %#", str);
}
// Check target URL exists
NSString *targetURLString = [params valueForKey:#"target_url"];
if (targetURLString) {
NSURL *targetURL = [NSURL URLWithString:targetURLString];
NSDictionary *targetParams = [self parseURLParams:[targetURL query]];
NSString *ref = [targetParams valueForKey:#"ref"];
// Check for the ref parameter to check if this is one of
// our incoming news feed link, otherwise it can be an
// an attribution link
if ([ref isEqualToString:#"notif"]) {
// Get the request id
NSString *requestIDParam = [targetParams
objectForKey:#"request_ids"];
NSArray *requestIDs = [requestIDParam
componentsSeparatedByString:#","];
// Get the request data from a Graph API call to the
// request id endpoint
[self notificationGet:[requestIDs objectAtIndex:0]];
}
}
// Clean out to avoid duplicate calls
self.openedURL = nil;
}
}
Is there any way that these problems are caused by the way that the app is not published on Appstore (Appstore id is not set neither for iPhone nor iPad)?
Here are code snippets showing using of the fb api:
Thank you very much for the time.
Enable deep linking in Facebook app settings
Facebook sdk 3.5 requests not working
I think this link will help you,configure App on Facebook as well

Authentication Error while extracting user information with Facebook Graph API

I am trying to extract user's basic information through FBConnect latest SDK. My code is simple:
- (void)viewDidLoad {
[super viewDidLoad];
facebook = [[Facebook alloc] initWithAppId:fbAppId];
NSArray* permissions = [[NSArray arrayWithObjects:#"user_about_me",#"read_stream",nil]retain];
[facebook authorize:permissions delegate:self];
[facebook requestWithGraphPath:#"me" andDelegate:self];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
fbAppId, #"app_id",
[NSString stringWithFormat: #"Some Text"], #"description",
#"My Test App", #"name",
#"Test Facebook Graph API",#"message",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
}
- (BOOL) application: (UIApplication*) application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void) fbDidLogin {
NSLog(#"FB didLogin");
}
- (void) fbDidNotLogin:(BOOL)cancelled {
NSLog(#"FB didNotLogin");
}
- (void) request:(FBRequest *)request didLoad:(id)result {
NSLog(#"request-didLoad-result");
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(#"received response");
}
Up till this point everything goes well apparently. Publish a feed through dialog on user's wall works fine. The problem occurs when I try to get user's information like name with:
[facebook requestWithGraphPath:#"me" andDelegate:self];
But neither fbDidLogin nor requestDidLoad is called. For requestDidLoad, I checked didLoadRawResponse as it is called before request didLoad:
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData*)data {
NSString *response = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Response is = %#",response);
[response release];
}
What I get in response is the following authentication error:
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
What is the reason and the solution?
I have added my whole code in the following wiki
How to retrieve Facebook response using Facebook iOS SDK
Hope this will help you, this is a working code if you have any question please let me know.

FBRequestDelegate didReceiveResponse how to find out for what request this response is

I am using facebook latest iphone sdk, I am making multiple request to facebook from different places in my app. For all of them didReceiveResponse and didLoad methods get called, it is very difficult to find out from didLoad method that for what request this response was so I am wondering if didReceiveResponse can help, can i retrieve some information in this method which will tell me what was the request for which I have got the response.
The way I do it is pretty much the same way Ziminji does it, but in didLoad method:
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(#"Facebook request %# loaded", [request url]);
//handling a user info request, for example
if ([[request url] rangeOfString:#"/me"].location != NSNotFound)
{
/* handle user request in here */
}
}
So basically you need only to check the url you sent the request to, and you can also check the parameters for that request. Then you can differentiate one from another.
All I am doing here is I am checking for some unique attribute in the response and relating it to the request, I know this is not the best way to do is, but this is what I have found so far, please let me know if any one is doing it any different
You could try something like the following:
- (void) request: (FBRequest *)request didReceiveResponse: (NSURLResponse *)response {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode == 200) {
NSString *url = [[response URL] absoluteString];
if ([url rangeOfString: #"me/feed"].location != NSNotFound) {
NSLog(#"Request Params: %#", [request params]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Facebook" message: #"Message successfully posted on Facebook." delegate: nil cancelButtonTitle: #"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
}
If you retain the request object as a property of your request delegate when you create it, you can check to see if it matches on the delegate method calls. For example:
- (void)queryForUserInfo {
self.userInfoRequest = [facebook requestWithGraphPath:#"me" andDelegate:self];
}
#pragma mark <FBRequestDelegate>
- (void)request:(FBRequest *)request didLoad:(id)result {
if (request == self.userInfoRequest) {
[self handleUserInfoResult:result];
}
}

Resources