I goes through many link and tutorials but not working for me.
Can anybody explain me How to use SLRequest for change via iOS to via MyAppName ?? step by step or give me some links which gives this solution in step by step.
EDIT: I have tried. Below is my code may be help.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{
ACFacebookAppIdKey: #"012345678912345",
ACFacebookPermissionsKey: #[#"publish_stream", #"publish_actions"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [accountStore
accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
}
else
{
// Handle Failure
}
}];
NSDictionary *parameters = #{#"message": #"My first iOS 6 Facebook posting "};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/feed"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
feedRequest.account = self->facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
// Handle response
}];
}
First of all set you facebook setting using Facebook developer account setting
then reset your simular or delete app from device.
Add and import this frameworks
#import <Accounts/Accounts.h>
#import <Social/Social.h>
#import <AdSupport/AdSupport.h>
#import <FacebookSDK/FacebookSDK.h>
use this code for further process
in yourclass.h file
#property (nonatomic, strong)ACAccountStore *accountStore;
in yourclass.m file
#synthesize accountStore;
- (IBAction)facebookButtonTouch:(id)sender {
if (self.accountStore == nil)
self.accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray * permissions = #[#"publish_stream", #"publish_actions",#"email"];
NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"your app id", ACFacebookAppIdKey, permissions, ACFacebookPermissionsKey, ACFacebookAudienceOnlyMe, ACFacebookAudienceKey, nil];
[self.accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *readPermissions = #[#"read_stream", #"read_friendlists"];
[options setObject:readPermissions forKey: ACFacebookPermissionsKey];
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if(granted && error == nil) {
/**
* We now should have some read permission
* Now we may ask for write permissions or
* do something else.
**/
} else {
NSLog(#"error is: %#",[error description]);
}
}];
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
}
else {
NSLog(#"error.localizedDescription======= %#", error.localizedDescription);
}
}];
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
//-------------- start code for posting message on wall via SLRequest------------------
NSDictionary *parameters = #{#"message": #"Hi Friends i m ....."};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/feed"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
feedRequest.account = facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(#"responseData %#",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}];
//-------------- end code for posting message on wall via SLRequest------------------
}
i hope it will help you. Thanks
Related
I have updated the version for the graph Api to v2. Now the issue I am facing is,when I executed the following code It shows me empty array :
FBRequest *friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *result, NSError *error) {
NSArray *friendshipid;
NSString *username;
if (!error) {
NSLog(#"friends = %#", [result description]);
}
if (completion) {
completion(friendshipid, username, error);
}
}];
}
}
I got to know that facebook sdk had some changes for the user friends and now It has to take the permission for the user_friends, but I have no idea where to make changes to ask for Permission for user_friends
You can use the given code snippet. Here, i have used the Classes of Social Framework to get the Facebook friends. Hope it will work for you.
- (void) connectWithFacebookFriends
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountTypeFacebook = [accountStore accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{ACFacebookAppIdKey: kFaceBookId,
ACFacebookPermissionsKey: #[#"user_friends"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[accountStore requestAccessToAccountsWithType:accountTypeFacebook
options:options
completion:^(BOOL granted, NSError *error)
{
if(granted) {
NSArray *accounts = [accountStore
accountsWithAccountType:accountTypeFacebook];
ACAccount* facebookAccount = [accounts lastObject];
NSString *acessToken = [NSString stringWithFormat:#"%#",facebookAccount.credential.oauthToken];
NSDictionary *parameters = #{#"access_token": acessToken};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *feedRequest =
[SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:feedURL
parameters:parameters];
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSString * str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
DLog(#"%#",str);
NSLog(#"Request failed, %#", [urlResponse description]);
}];
} else {
NSLog(#"Access Denied");
NSLog(#"[%#]",[error localizedDescription]);
}
}];
}
I am trying to get facebook friends list through social framework.
- (void)getfriends {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *emailReadPermisson = #{
ACFacebookAppIdKey : #"1537296106498495",
ACFacebookPermissionsKey : #[#"user_friends"],
ACFacebookAudienceKey : ACFacebookAudienceEveryone,
};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:emailReadPermisson completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [accountStore
accountsWithAccountType:facebookAccountType];
_facebookAccount = [accounts lastObject];
NSString *acessToken = [NSString stringWithFormat:#"%#",_facebookAccount.credential.oauthToken];
NSDictionary *parameters = #{#"access_token": acessToken};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:feedURL
parameters:parameters];
feedRequest.account = _facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(#"%#",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}];
}
}];
}
But data field in the response is empty. My response is:
2014-08-06 18:09:11.459 Social[5246:1303] {"data":[],"summary":{"total_count":1115}}
This is my code to post a video to facebook:
ACAccountStore *accountStore = [[ACAccountStore alloc]init];
NSArray * p = [NSArray arrayWithObjects:#"email",#"basic_info", nil];
NSDictionary * dict=[NSDictionary dictionaryWithObjectsAndKeys:#"xxx",ACFacebookAppIdKey,p,ACFacebookPermissionsKey,ACFacebookAudienceEveryone,ACFacebookAudienceKey, nil];
NSDictionary *options = #{
ACFacebookAppIdKey: #"xxx",
ACFacebookPermissionsKey: #[#"publish_actions",#"publish_stream, " ],
ACFacebookAudienceKey: ACFacebookAudienceFriends,
};
ACAccountType *facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:facebookAccountType options:dict completion:^(BOOL granted, NSError *error) {
NSLog(#"blablalba");
//do nothing here.
if (granted) {
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(#"posting to fb..");
NSURL *url = [NSURL URLWithString:#"https://graph.facebook.com/me/videos"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSURL *videoPathURL = [defaults URLForKey:#"vidURL"];
// NSURL *videoPathURL = [[NSURL alloc]initFileURLWithPath:videoPath isDirectory:NO];
// NSData *videoData = [NSData dataWithContentsOfFile:videoPath];
NSData *videoData = [NSData dataWithContentsOfFile:[videoPathURL path]];
NSString *status = #"One step closer.";
NSDictionary *params = #{#"title":status, #"description":status};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:url
parameters:params];
[request addMultipartData:videoData
withName:#"source"
type:#"video/quicktime"
filename:[videoPathURL absoluteString]];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (error) {
NSLog(#"error: %#", error);
}
NSLog(#"httpresponse: %#", urlResponse);
}];
}
}];
}
}];
But its returning:
"OAuth \"Facebook Platform\" \"invalid_request\" \"An active access token must be used to query information about the current user.\"";
I have no idea what I'm missing. Any help?
You are forgetting to set the facebook account to the request.
When you are granted the access, fetch the account:
NSArray *accounts = [self.accountStore
accountsWithAccountType:fbAccountType];
self.facebookAccount = [accounts lastObject];
and after creating the request, set the account
[request setAccount:self.facebookAccount];
Found that I had to add the token to the params field:
NSString *status = #"One step closer.";
NSDictionary *params = #{#"title":status, #"description":status, #"access_token":accessToken};
Hope it helps others.
I'm using below code to get facebook friends list, and its working fine. But there is a need of friends profile picture too.
NSString *fbPermissions = #[ #"user_about_me",#"email",#"user_friends"];
NSString *apiIdKey = #"App_Key";
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
id options = #{
ACFacebookAppIdKey: apiIdKey,
ACFacebookPermissionsKey: fbPermissions,
ACFacebookAudienceKey: ACFacebookAudienceFriends,
};
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];
ACAccount *tempAccount = [arrayOfAccounts lastObject];
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends"];
NSURL *fbURL = [NSURL URLWithString:string];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:fbURL
parameters:nil];
[fbRequest setAccount:tempAccount];
[fbRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error)
{
NSError *jsonError = nil;
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSMutableArray *array = [data valueForKey:#"data"];
NSLog(#"Friends array:- %#",array);
}
else
{
[[[UIAlertView alloc]initWithTitle:#"Error"
message:[error description]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil] show];
}
}];
}
}];
To get profile picture I have changed request url with:-
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends?fields=picture,name"];
I got following response :-
{
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}
I'm not sure its access token expiration error because to revert back request Url to previous one, code works fine.
Finally it worked with:-
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends"];
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET URL:requestURL parameters:#{#"fields":#"id,name,picture,first_name,last_name,gender"}];
I am trying to upload a video on an iPhone to the user's facebook timeline using Social Framework. I used the snippet given in this answer. Here is my code:
- (void)performActivity {
NSLog(#"sharing on facebook.");
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{ACFacebookAppIdKey : #"***", ACFacebookPermissionsKey : #[#"email"], ACFacebookAudienceKey:ACFacebookAudienceFriends};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSDictionary *options = #{ACFacebookAppIdKey : #"***", ACFacebookPermissionsKey : #[#"publish_stream"], ACFacebookAudienceKey:ACFacebookAudienceFriends};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(#"Publishing");
ACAccount *fbAccount = [accountStore accountsWithAccountType:facebookAccountType][0];
NSURL *videourl = [NSURL URLWithString:#"https://graph.facebook.com/me/videos"];
NSURL *pathURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#session.mov", NSTemporaryDirectory()]];
NSData *videoData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:#"%#session.mov", NSTemporaryDirectory()]];
NSDictionary *params = #{
#"title": #"Nebuu",
#"description": #"mebuu"
};
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:videourl
parameters:params];
[uploadRequest addMultipartData:videoData
withName:#"source"
type:#"video/quicktime"
filename:#"Nebuu Video"];
uploadRequest.account = fbAccount;
[uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(error){
NSLog(#"Error %#", error.localizedDescription);
}else
NSLog(#"%#", responseString);
}];
}}];
}}];
}
As you can see, I first requested read permission(email) then, requested publish permission if the first request is granted. The code works fine, all the permissions are granted and the app begins uploading video(I can see this in my network monitoring tool). However, after video is uploaded I get this error:
{
"error": {
"message": "An unexpected error has occurred. Please retry your request later.",
"type": "OAuthException"
}
}
What might be the problem here? I read some other SO posts and seems it is possible thet the error is originated from Facebook's side. Any suggestions?