Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook in iOS6.0 use SLRequest to upload a photo failed anyway

Here Comes my Objc code:

ACAccountStore *facebookaccount = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = @{ ACFacebookAppIdKey: @"1234567899876543", ACFacebookPermissionsKey: @[@"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
    if(granted) {
        NSArray *accountsArray = [facebookaccount accountsWithAccountType:facebookaccountType];
        if ([accountsArray count] > 0) {
            ACAccount *facebookAccount = [accountsArray objectAtIndex:0];

            NSString *sendmessage = @"Face";
            NSData *myImageData = UIImagePNGRepresentation(imageSource);

            SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"] parameters:nil];

            [facebookRequest addMultipartData:myImageData withName:@"source" type:@"multipart/form-data" filename:nil];
            [facebookRequest addMultipartData:[sendmessage dataUsingEncoding:NSUTF8StringEncoding] withName:@"message" type:@"multipart/form-data" filename:nil];

            [facebookRequest setAccount:facebookAccount];

            [facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
                if (error == nil) {
                    NSLog(@"responedata:%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
                }else{
                    NSLog(@"%@",error.description);
                }
        }
    }
    else
    {
        NSLog(@"error description : %@",[NSString stringWithFormat:@"%@", error.localizedDescription]);
    }
}];

Finally I get these respone data:

responedata:{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}

Help me please!!!

like image 319
user1800746 Avatar asked Nov 05 '12 17:11

user1800746


1 Answers

I can successfully upload a photo by including a file name in addMultipartData and by passing the message as part of the SLRequest options.

code:

NSDictionary *parameters = @{@"message": sendmessage};

SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                            requestMethod:SLRequestMethodPOST
                                                      URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"]
                                                           parameters:parameters];

[facebookRequest addMultipartData: myImageData
                                     withName:@"source"
                                         type:@"multipart/form-data"
                                     filename:@"TestImage"];

facebookRequest.account = facebookAccount;

[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
     // Log the result
}];
like image 117
Mitch Avatar answered Nov 07 '22 22:11

Mitch