Objective-C Facebook video sharing for the app - ios

I'm very much new to objective-c. Basically, all I see is black right now. I'm assigned to code the facebook sharing part of the app we're making, and believe me, I've tried almost all tutorials (none worked, I've tried) and went through the questions here in stack, and I still couldn't upload. So please, someone help?
How to upload videos to facebook via the app? Please provide sample code if you have.
Here is the code I'm trying right now:
-(void)post
{
NSURL *videourl = [NSURL URLWithString:#"https://graph.facebook.com/me/videos"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Here's to the Crazy Ones" ofType:#"mp4"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSLog(#"PATH: %#", filePath);
NSDictionary *params = #{
#"title": #"uploading",
#"description": #"testing..."
};
NSLog(#"uploading...");
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:videourl
parameters:params];
[uploadRequest addMultipartData:videoData
withName:#"source"
type:#"video/quicktime"
filename:[pathURL absoluteString]];
NSLog(#"Here it goes...");
uploadRequest.account = self.facebookAccount;
NSLog(#"Past uploadrequest...");
[FBSession setActiveSession:FBSession.activeSession];
[uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// [FBSession setActiveSession:FBSession.activeSession];
if (!FBSession.activeSession.isOpen) {
// [FBSession openActiveSessionWithAllowLoginUI: YES];
[FBSession setActiveSession:FBSession.activeSession];
}
NSLog(#"response string: %#", responseString);
NSLog(#"Here I am...");
if(error){
NSLog(#"Error %#", error.localizedDescription);
}else
NSLog(#"%#", responseString);
}];
}
With this I'm getting an error of:
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}

Here I provide a link which will guide you step step to upload video to FB Tutorial

Related

How can I test facebooks friends tagging

I want my app to tag friend within a video-post.
(Posting a video without the tags is working fine)
The account I'm using, has been added to the list of testers for my app.
So I should be able to do this call without any permissions.
But the response is still:
"error: (#3) Application does not have the capability to make this API call."
To get the "Taggable Friends" permission, I need upload a build of my app to fb.
But I can't implement this feature without testing it by myself first.
How can I solve this chicken-and-egg problem?
My code looks like this (iOS - Objc):
NSDictionary *params = #{
#"title": #"some title",
#"description": #"some desctiption",
#"tags": _friendId
};
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:videourl
parameters:params];
I have found the solution.
You can't set the tag whithin the upload request (I still don't know why).
But you can tag a user in the video after you uploaded the video.
When the upload succeeded, you get a videoId:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableContainers
error:&error];
_currentVideoId = json[#"id"];
With this id you can make a second request:
ACAccountCredential *fbCredential = [_facebookAccount credential];
NSString *accessToken = [fbCredential oauthToken];
NSString *urlStr = [NSString stringWithFormat: #"https://graph-video.facebook.com/%#/tags?access_token=%#&tag_uid=%#", _currentVideoId, accessToken, userId];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString: urlStr]
parameters: #{}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//Do something
}];
});
To make the second request, you need the permission for #"user_videos".
If you want to tag more users, you need to make this request a few times. Maybe it's also possible with one request, but I didn't figuered out how to do it yet.

How to post image with private tweet using SLRequest in iOS?

I have a problem that I have some followers and I want to send a direct message with image to all followers but I didn't get a way to do that.
I study whole documentation but can't find a way to do that.
My code is here:
NSString *strUrl = #"https://api.twitter.com/1.1/direct_messages/new.json";
NSURL *url = [NSURL URLWithString:strUrl];
strUrl = nil;
NSMutableDictionary *parameters = [NSMutableDictionary new];
[parameters setValue:#"name" forKey:#"screen_name"];
[parameters setValue:#"123456" forKey:#"user_id"];
[parameters setValue:#"sending text" forKey:#"text"];
// Creating a request.
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:url
parameters:parameters];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.greenuplawnandsprinklers.com/uploads/design_sample_landscape.jpg"]];
[request addMultipartData:imageData
withName:#"media[]"
type:#"image/jpeg"
filename:#"image.jpg"];
imageData = nil;
[request setAccount:twitAccount];
url = nil;
parameters = nil;
// Perform the request.
[request performRequestWithHandler:^(NSData *responseData,NSHTTPURLResponse *urlResponse,NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
// Check if we reached the rate limit.
if ([urlResponse statusCode] == 429)
{
NSLog(#"Rate limit reached");
return;
}
if(LOGS_ON) NSLog(#"TwitterFriendModel-->shareMagazineTitle-->error = %#",error);
// Check if there is some response data.
if (responseData)
{
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
NSLog(#"dictionary = %#",dictionary);
dictionary = nil;
}
});
}];
}
I saw all other similar question/answers on stackoverflow but I didn't get a way to send direct message with image to followers.
Please don't consider this question as duplicate and try to resolve my problem. I will appreciate your every clue.
Here is my code to accomplish this task, it works for me.. hope also helpfull for you..
NSDictionary *message = #{#"status": messagetext};
NSURL *requestURL = [NSURL
URLWithString:#"https://upload.twitter.com/1/statuses/update_with_media.json"];
SLRequest *postRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:requestURL parameters:message];
UIImage *image = [self imageReduceSize:[UIScreen mainScreen].bounds.size:imgPost.image];
NSData *myData = UIImagePNGRepresentation(image);
[postRequest addMultipartData:myData withName:#"media" type:#"image/png" filename:#"TestImage"];

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?

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

Uploading video to facebook from iOS -- unable to request log in permission from user, error code 6

I'm using the social framework in iOS 6 to integrate facebook video uploading into my app. However, nothing happens when I run the program, and I instead receive these errors:
The operation couldn’t be completed. (com.apple.accounts error 6.)
"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}"
Below is my code, the first half of which I implemented from this tutorial. granted is set to false, causing the first error.
- (void)uploadToFb {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{
ACFacebookAppIdKey: #"2************",
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
{
NSLog(#"Error %#", e.localizedDescription); }
}
}];
NSURL *videourl = [NSURL URLWithString:#"https://graph.facebook.com/me/videos"];
NSString *filePath = [outputURL path];
NSURL *pathURL = outputURL;
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *params = #{
#"title": #"Face Puppets Video",
#"description": #"A funny video I made with the #FacePuppets iOS app."
};
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:videourl
parameters:params];
[uploadRequest addMultipartData:videoData
withName:#"source"
type:#"video/quicktime"
filename:[pathURL absoluteString]];
uploadRequest.account = 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);
}];
}
Hi Here it is a very nice Sharing example iOS (Wiki) (Sample Project for iOS, ~3 MB) Using this you can share Video in to Facebook, YouTube, Flicker, Vimeo Take a look above link and test this Example.
You need to set You AppId in to ESSViewController.m class In this Method:
- (IBAction)facebook:(id)sender
{
self.fb = [[[ESSFacebook alloc] initWithDelegate:self
appID:#"youAppID"
appSecret:#"secret"] autorelease];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"IMG_1203" withExtension:#"MOV"];
[self.fb uploadVideoAtURL:url];
}
Might Be this is an Old virsion of Facebook but for getting Idea about how to process for share Video from ourApp.
All of your code after NSURL *videourl should be moved to another method. Currently it is being executed while the requestAccessToAccountsWithType: request is in progress so the facebookAccount reference isn't available yet. So, the new method that you create and move that code to should be called from the completion block if granted is true.
Note hat you can't just immediately request publish permissions. You must request read permissions first and then, once it's been granted, request publish permissions.
The problem seems to be with your NSDictionary creation.
NSDictionary *options = #{
ACFacebookAppIdKey: #"2************",
ACFacebookPermissionsKey: #[#"publish_stream", #"publish_actions"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
you see you are passing keys first and objects later, where NSDictionary calls +dictionaryWithObjectsAndKeys in this case...
try using the correct format like:
NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
#"###############", ACFacebookAppIdKey,
[NSArray arrayWithObject:#"publish_stream"], ACFacebookPermissionsKey,
nil];
this should work.
see the same question here:
Getting "Error Code 8" When Calling [ACAccountStore requestAccessToAccountsWithType] - iOS Facebook

Resources