Get Facebook uid from ACAccountStore in iOS? - iphone

Hi i want to get the Facebook user's UID from ACAccountStore in iOS 6
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = #"01234567890123";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,#[#"email"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
self.facebookAccount = [accounts lastObject];
//i Want to get the Facebook UID and log it here
NSLog(#"facebook account =%#",self.facebookAccount);
} else {
//Fail gracefully...
NSLog(#"error getting permission %#",e);
}
}];
self.facebookAccount has UID, but i am unable to get it...

I wanted to get the UID without using the Facebook API. It can done by using the following method
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = #"01234567890123";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,#[#"email"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
self.facebookAccount = [accounts lastObject];
//i got the Facebook UID and logged it here (ANSWER)
NSLog(#"facebook account =%#",[self.facebookAccount valueForKeyPath:#"properties.uid"]);
} else {
//Fail gracefully...
NSLog(#"error getting permission %#",e);
}
}];

You need to call the Facebook Graph API; use the SLRequest as follows:
NSUrl requestURL = NSUrl.FromString("https://graph.facebook.com/me");
SLRequest sl = SLRequest.Create(SLServiceKind.Facebook, SLRequestMethod.Get, requestURL, null);
sl.Account = // your ACAccount object here
sl.PerformRequest((data, response, error) => {
if (error == null) {
// success! parse response
}
else {
// handle failure
}
});
That should do it!

Related

What should I write in place of ACFacebookAppIdKey?

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAcc = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{ (NSString *)ACFacebookAppIdKey: #"##########",
(NSString *)ACFacebookPermissionsKey: [NSArray arrayWithObject:#"email"],
(NSString *)ACFacebookAudienceKey: ACFacebookAudienceEveryone,
};
[accountStore requestAccessToAccountsWithType:facebookAcc options:options completion:^(BOOL granted, NSError *error){
if (granted) {
ACAccount *facebookAccount = [[accountStore accountsWithAccountType:facebookAcc] firstObject];
NSLog(#"facebook usre name: %#, Full name: %#", facebookAccount.username, facebookAccount);
}
else if (error)
NSLog(#"Error occured:%#", [error localizedDescription]);
}];
}
and I am getting
Error occurred:The Facebook server could not fulfill this access request: invalid app id
what should I write ACFacebookAppIdKey
go to https://developers.facebook.com/. Create new a new app for iOS or choose your app. At the Dashboard menu, you can see your App ID. This App ID you should put to the ACFacebookAppIdKey.

app id in slrequest

Is it mandatory to use app Id while integrating with Facebook using SLRequest actually I don't have the App id,so is it possible to integrate Facebook using SLRequest without using app id as i dont have a registered itunes account?
-(void)viewDidLoad
{
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierFacebook];
NSString *key = #"987654";
NSDictionary *dictFB = //use ACAccountStore to help create your dictionary
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB
completion: ^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with SSO
self.facebookAccount = [accounts lastObject];
}
else {
//Fail gracefully...
NSLog(#"error getting permission %#",e);
}
}];
Here the appId means the Facebook Client Id. In Facebook developer account , you will get the App ID/API Key, where you would have created your application.

Twitter integration issue with ACAccountStore (iOS 5)

When I run below code with iOS 6.0, Its working
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (granted)
{
//MY CODE
}
});
}];
and When I run this code with iOS 5.0 or 5.1, It crashes with following output,
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[ACAccountStore requestAccessToAccountsWithType:options:completion:]:
unrecognized selector sent to instance 0x68a57c0'
Don't know about this weird crash log..
Please tell me, How to get rid of this..
Use below method:
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted) {
//Your code
}
}
}];
This is a bit late, but the reason you're getting that error is that requestAccessToAccountsWithType:options:completion: is new in iOS 6.
In iOS 5 and earlier use the requestAccessToAccountsWithType:withCompletionHandler method instead (this method is deprecated in iOS 6)
See the docs: https://developer.apple.com/library/ios/documentation/Accounts/Reference/ACAccountStoreClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40011021-CH1-SW12
Try update for this:
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// iOS 6
if ( [account respondsToSelector:#selector(requestAccessToAccountsWithType: options: completion:)] )
{
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (granted)
{
//MY CODE
}
});
}];
}
// iOS 5
else if ( [account respondsToSelector:#selector(requestAccessToAccountsWithType: withCompletionHandler:)] )
{
[account requestAccessToAccountsWithType:accountType
withCompletionHandler:^(BOOL granted, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (granted)
{
//MY CODE
}
});
}];
}
else
{
// iOS 4 or less
}
Thanks #CReaTuS, I want to clarify this up to some more bit, Note that in case of iOS6 we make SLRequest where in iOS5 we have to perform request using TWRequest. See below-
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if ( [accountStore respondsToSelector:#selector(requestAccessToAccountsWithType: options: completion:)] )
{
[accountStore requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (granted)
{
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"Twitter_Name" forKey:#"screen_name"];
[tempDict setValue:#"true" forKey:#"follow"];
//Code specific to iOS6 or later
SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"https://api.twitter.com/1.1/friendships/create.json"] parameters:tempDict];
// To unfollow hit URL-https://api.twitter.com/1.1/friendships/destroy.json
[followRequest setAccount:twitterAccount];
[followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
NSLog(#"%#", output);
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request failed
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show success
});
}
}];
}
}
});
}];
}
else if ( [accountStore respondsToSelector:#selector(requestAccessToAccountsWithType: withCompletionHandler:)] )
{
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (granted)
{
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"Twitter_Name" forKey:#"screen_name"];
[tempDict setValue:#"true" forKey:#"follow"];
//Code specific to iOS5
TWRequest *followRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"https://api.twitter.com/1/friendships/create.json"]
parameters:tempDict
requestMethod:TWRequestMethodPOST];
[followRequest setAccount:twitterAccount];
[followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
NSLog(#"%#", output);
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request failed
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show success
});
}
}];
}
}
});
}];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request completely failed
});
}
Happy coding :)

An active access token must be used to query information about the current user

I am trying to fetch user details but am currently unable to fetch images.This is the error I am getting:
{
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}
This is my code:
if(!self.store)
{
ACAccountStore *store1 = [[ACAccountStore alloc] init];
self.store=store1;
[store1 release];
}
ACAccountType *fbAccountType =
[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray * permissions = #[#"read_stream", #"publish_stream",#"email", #"user_birthday",#"publish_actions",#"user_photos"];
NSDictionary * dict = #{ACFacebookAppIdKey : #"my_key", ACFacebookPermissionsKey : permissions, ACFacebookAudienceKey : ACFacebookAudienceOnlyMe};
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:fbAccountType options:dict completion:^(BOOL granted, NSError *error) {
__block NSString * statusText = nil;
if (granted) {
statusText = #"Logged in";
NSArray * accounts = [store accountsWithAccountType:fbAccountType];
store = [accounts lastObject];
account = [accounts objectAtIndex:0];
NSLog(#"account is: %#", account);
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me?fields=picture"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = account;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error){
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions error:&error];
NSLog(#"Dictionary contains: %#", list );
}
else{
//handle error gracefully
}
}];
}
If I use https://graph.facebook.com/me as url then it works fine. But I need the profile pic as well. What to do?
"fields" is a parameter, not a part of the URL itself. This works:
SLRequest *videoLimitsRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:#"https://graph.facebook.com/me"]
parameters:#{#"fields": #"video_upload_limits"}];
FYI, token is added automatically when you attach account to the SLRequest.
you can run fql with accesstoken like this
https://graph.facebook.com/fql?q=SELECT name from user where uid = YourFAcebookID&access_token=ACCESSTOKEN
This will work for you.
This is a bit old but I forgot to answer then.
From FB api 3.2 onwards everything is managed within the api, for bot iOS 5 and 6 native login. Here is what I did:
FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
FBRequest *request = [FBRequest requestWithGraphPath:#"me/albums" parameters:nil HTTPMethod:#"GET"];
FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error)
{
if(!error)
{
NSDictionary *list =(NSDictionary*)result;
int flag = 0;
for (int index = 0; index < [[list objectForKey:#"data"] count];index++)
{
if ([[[[list objectForKey:#"data"] objectAtIndex:index] objectForKey:#"name"] isEqualToString:#"Profile Pictures"])
{
[self fetchAlbumImages:[[[list objectForKey:#"data"] objectAtIndex:index] objectForKey:#"id"]];
flag = 1;
}
}
if (flag == 0)
{
[self fetchAlbumImages:#"No Album"];
}
}
else
{
}
};
[newConnection addRequest:request completionHandler:handler];
//[self.requestConnection cancel];
[newConnection start];
[newConnection release];
}
- (void) fetchAlbumImages:(NSString*)albumId
{
if ([albumId isEqualToString:#"No Album"])
{
NSMutableArray *albumArray = [[NSMutableArray alloc]init];
[self performSelectorOnMainThread:#selector(sendRegistrationRequest:) withObject:albumArray waitUntilDone:YES];
[albumArray release];
}
else
{
FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
FBRequest *request = [FBRequest requestWithGraphPath:[NSString stringWithFormat:#"%#/photos",albumId] parameters:nil HTTPMethod:#"GET"];
FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error)
{
if(!error)
{
NSDictionary *list =(NSDictionary*)result;
NSMutableArray *albumArray = [[NSMutableArray alloc]init];
for (int index = 0; index < ([[list objectForKey:#"data"] count]<10?[[list objectForKey:#"data"] count]:10);index++)
{
[albumArray addObject:[[[list objectForKey:#"data"] objectAtIndex:index] objectForKey:#"source"]];
}
[self performSelectorOnMainThread:#selector(sendRegistrationRequest:) withObject:albumArray waitUntilDone:YES];
}
else
{
}
};
[newConnection addRequest:request completionHandler:handler];
//[self.requestConnection cancel];
self.requestConnection = newConnection;
[newConnection start];
[newConnection release];
}
}

SLRequest performRequestWithHandler does not work in ios 6

I am trying to build simple prototype where I post some text to my facebook account. I have read the ios 6 facebook integration documentation and came up with following code. Everything seems to work fine until I hit the last block where I create SLRequest object in method postTextToFacebook and try to execute performRequestWithHandler with handler block. Control never does inside the handler block. I am assuming that performRequestWithHandler call is not successful in this case. Any one have done with successfully? Here is code for your reference.
#import <Social/Social.h>
#import "ViewController.h"
#implementation ViewController
#synthesize facebookAccount;
#synthesize accountStore;
#synthesize textToPost;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction) postToFacebook:(id)sender
{
self.statusLabel.text = #"Logging in ...";
if(self.accountStore == nil)
{
self.accountStore = [[ACAccountStore alloc] init];
}
ACAccountType *facebookAccountType = [self.accountStore enter code hereaccountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSMutableDictionary *myOptions = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"172197712918906", ACFacebookAppIdKey,
[NSArray arrayWithObjects:#"email", #"user_about_me", #"user_likes", nil], ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:myOptions completion:^(BOOL granted, NSError *error){
__block NSString *statusText;
if(granted)
{
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
self.facebookAccount = [accounts lastObject];
[myOptions setObject:[NSArray arrayWithObjects:#"publish_stream", nil] forKey:ACFacebookPermissionsKey];
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:myOptions completion:^(BOOL granted, NSError *error) {
__block NSString *statusText1;
if(granted && error == nil)
{
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
self.facebookAccount = [accounts lastObject];
[self postTextToFacebook];
statusText1 = #"Text Posted.";
}
else{
statusText1 = #"Publish Failed.";
}
dispatch_async(dispatch_get_main_queue(), ^{
self.statusLabel.text = statusText1;
});
}];
}
else{
statusText = #"Login Failed.";
NSLog(#"Error = %#",error);
}
}];
}
-(void) postTextToFacebook
{
NSDictionary *parameters = #{#"message":self.textToPost.text};
NSURL *feedURL = [NSURL URLWithString:#"https://graphs.facebook.com/me/feed"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:feedURL parameters:parameters];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(#"Facebook request received, status code %d", urlResponse.statusCode);
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Response data: %#", response);
//handle errors
if(error == nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
self.statusLabel.text = #"text posted to facebook";
});
}
}];
}
#end
Your url should be
https://graph.facebook.com/me/feed
instead of
https://graphs.facebook.com/me/feed
I was having a same issue and got an NSURLErrorDomain -1003 error on it.