Getting OauthException when uploading a video to facebook - ios

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?

Related

User friends are showing empty list in Facebook Sdk Graph Api v2

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]);
}
}];
}

Upload video to Facebook doesn't work with SLRequest

I was trying to upload the video to Facebook with following code:
- (void)facebok
{
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:#"com.apple.facebook"];
NSDictionary *options = #{
ACFacebookAppIdKey: #"appid",
ACFacebookPermissionsKey: #[#"publish_stream"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:options 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];
NSLog(#"facebook account =%#",self.facebookAccount);
[self post];
} else {
//Fail gracefully...
NSLog(#"error getting permission %#",e);
}
}];
}
- (void)post
{
NSURL *url = [NSURL URLWithString:#"https://graph.facebook.com/me/videos"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mov"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSString *status = #"One step closer.";
NSDictionary *params = #{#"title":status, #"description":status};
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:url
parameters:params];
[uploadRequest addMultipartData:videoData
withName:#"source"
type:#"video/quicktime"
filename:[pathURL absoluteString]];
uploadRequest.account = self.facebookAccount;
[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);
}];
}
Fist it ask for the permission to post the video. That is fine but after few seconds it gives me this error:
{"error":{"message":"You recently posted something that violates Facebook policies, so you're temporarily blocked from using this feature. For more information, visit the Help Center.\n\nTo keep from getting blocked again, please make sure you've read and understand Facebook's Community Standards.","type":"OAuthException","code":368}}
Am I doing something wrong to post the video on Facebook?

An active access token must be used to query information about the current user error when posting video to facebook

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.

Can't able to fetch the User Profile photo in ios

I am trying to get the facebook account details using SLRequest in ios.
I got all the details But Can not able to get the Profile Photo link of the User.
My code is as follow:
NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
#"xxxxxxxxxxxxxx", ACFacebookAppIdKey,
[NSArray arrayWithObjects:#"email", nil] , ACFacebookPermissionsKey,
nil];
[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:options
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
NSLog(#"Success");
[self me];
}else{
// ouch
NSLog(#"Fail");
NSLog(#"Error: %#", error);
}
}];
- (void)me{
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = _facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#", meDataString);
NSJSONSerialization *js=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
NSLog(#"%#",js);
}];
}
Help me to solve this.
You can get the user's ID from the returned response, then use the below to get the profile picture:
http://graph.facebook.com/User_ID/picture
I use SDWebImage for images:
NSString *urlstr = #"http://graph.facebook.com/100001393655684/picture";
[imageView setImageWithURL:[NSURL URLWithString:urlstr]
placeholderImage:nil];
For Twitter, please make sure you authenticate your request, check this link for more details
Note: You may need to update TWRequest to SLRequest if you aren't deploying for iOS5

How to use SLRequest for change via iOS to via <AppName>

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

Resources