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
Related
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?
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 am trying to share data with facebook. I am able to share data except image stored locally in iPhone memory. But when I try to share image stored locally with parameter #"source" my app gets terminated with error "[NSConcreteMutableData _fastCharacterContents]".
For sharing a local image I am converting it into NSData as required by #"source" parameter.
NSData *data = [NSData dataWithContentsOfURL:localUrl];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{
ACFacebookAppIdKey: #"xxxxxxxxxxxxxx",
ACFacebookPermissionsKey: #[#"email"],
ACFacebookAudienceKey: ACFacebookAudienceEveryone
}; // basic read permissions
[self.accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *e)
{
if (granted) {
// Now that you have publish permissions execute the request
NSDictionary *options2 = #{
ACFacebookAppIdKey: #"xxxxxxxxxxxxxx",
ACFacebookPermissionsKey: #[#"publish_stream", #"publish_actions"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options2 completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
/*NSDictionary *parameters = #{#"message": #"This is a test",
#"name": #"Sharing Tutorial",
#"caption": #"Build great social apps and get more installs.",
#"description": #"Allow your users to share stories on Facebook from your app using the iOS SDK.",
#"link": #"https://developers.facebook.com/docs/ios/share/",
#"source":data};*/
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"My hat image", #"message", data, #"source", nil];
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/photos"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
NSLog(#"AnythingHere?");
[feedRequest setAccount:facebookAccount];
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
// Handle response
NSLog(#"%#%#", error,urlResponse);
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
NSLog(#"Facebook Response : %#",response);
}];
}
else {
NSLog(#"Access denied 2");
NSLog(#"%#", [error description]);
}
}];
} else {
NSLog(#"Error: %#", [e description]);
NSLog(#"Access denied");
}
}];
What can be the reason behind this error?
This is just an educated guess because I've never used the Social framework, but it's too long for a comment.
Judging by this answer I would say that you are using SLRequest wrong. I'm pretty sure that the exception comes from your data object which is interpreted as a NSString (or NSURL).
Maybe you should use addMultipartData:withName:type:filename: to attach your photo to the request.
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"My hat image", #"message", nil];
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/photos"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
[feedRequest addMultipartData:data
withName:#"source"
type:#"multipart/form-data"
filename:#"Test Image"];
which is basically the same code as another answer
please recheck your parameters formation.
and convert your image in following formate(Base64)
NSData *rep = UIImagePNGRepresentation(img);
NSLog(#"Rep: %#", rep);
NSString *base64 = [rep encodeBase64];
NSLog(#"Base 64 is a %#", NSStringFromClass([base64 class]));
self.somestring = [#"hardcodedstring" stringByAppendingString:base64]; //encodeBase64 encodes it to base64
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?
I'm developing a Twitter Feed View for an iOS App. I found TWRequest and it works exactly like that which i was looking for. But: i get an Notice: "TWRequest is deprecated: first deprecated in iOS 6.0". What should I use instead?
On iOS 6 you should use the Social.framework. This has a class named SLRequest.
You use it almost in the same way as the deprecated TWRequest, but you need to specify that it's a twitter request as opposed to a facebook request.
The entire Twitter.framework became deprecated as of iOS 6, since Apple added Facebook and Weibo (a Chinese social network) to iOS 6, they grouped all social classes into the new Social.framework.
Note you must specify the service type for Twitter/Facebook, Example:
SLRequest *aRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:myurl
parameters:myparams];
Be sure to check out the documentation.
Here is a complete code to upload text + image to your Twitter Account using Twitter api
UIImage *img = [UIImage imageNamed:#"twitterImage.png"];
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted == YES) {
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0) {
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
NSDictionary *message = #{#"status": #"From my app"};
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];
NSData *data = UIImagePNGRepresentation(img);
[postRequest addMultipartData:data withName:#"media" type:#"image/png" filename:#"TestImage"];
postRequest.account = acct;
[postRequest performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse
*urlResponse, NSError *error)
{
if (error) {
NSLog(#"%#",error.description);
}
else {
NSLog(#"Upload Sucess !");
}
}];
}
}
}];
In case you plan on integrating TwitterKit by Twitter to perform the tweets via your custom twitter app then this might help you.
https://stackoverflow.com/a/28602749/1740354
Another alternative is to use Twitter API. You should have Twitter framework for that.
Then do following code:
NSString *statusesShowEndpoint = #"https://api.twitter.com/1.1/statuses/update.json";
NSDictionary *params = #{#"status": #"Hello, my first autopost tweet..."};
NSError *clientError;
NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
URLRequestWithMethod:#"POST"
URL:statusesShowEndpoint
parameters:params
error:&clientError];
if (request) {
[[[Twitter sharedInstance] APIClient]
sendTwitterRequest:request
completion:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
if (data) {
// handle the response data e.g.
NSError *jsonError;
NSDictionary *dicResponse = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&jsonError];
NSLog(#"%#",[dicResponse description]);
}
else {
NSLog(#"Error code: %ld | Error description: %#", (long)[connectionError code], [connectionError localizedDescription]);
}
}];
}
else {
NSLog(#"Error: %#", clientError);
}