如何在发布使用SLRequest在Facebook上传视频的权限之前获得读取权限?

时间:2013-06-05 18:37:45

标签: iphone ios facebook acaccount slrequest

我只是想将Facebook共享集成到我的应用程序中,我的应用程序的最终产品是一个视频,因此上传和与朋友分享最终的编译是我的想法,我使用ACAccount框架的本机社交框架,为我的应用程序工作似乎是Facebook的奇怪逻辑,我需要请求读取权限,然后我要求获得publish_stream权限,并且他们不能紧接着(在某处读取),我的问题是我应该在哪里以及如何在我的实现这个逻辑代码,继承我的代码

    ACAccountStore* accountStore = [[ACAccountStore alloc] init];
NSDictionary *options = @{
                          ACFacebookAppIdKey: @"My app ID",
                          ACFacebookPermissionsKey: @[@"user_birthday"],
                          ACFacebookAudienceKey: ACFacebookAudienceOnlyMe
                          };
ACAccountType *facebookAccountType = [accountStore
                                      accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {

    if (granted) {

        NSArray *accounts = [accountStore
                             accountsWithAccountType:facebookAccountType];
         ACAccount * facebookAccount = [accounts lastObject];



        NSLog(@"access to facebook account ok %@", facebookAccount.username);

        NSData *videoData = [NSData dataWithContentsOfFile:[_fURL absoluteString]];
        NSLog(@"%@",[_fURL absoluteString]);
        NSLog(@"video size = %d", [videoData length]);
        NSDictionary *params = @{
                                 @"title": @"Me being silly",
                                 @"description": @"Me testing the video upload to Facebook with the new system."
                                 };

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

        [request addMultipartData:videoData
                         withName:@"source"
                             type:@"video/quicktime"
                         filename:[_fURL absoluteString]];
        request.account = facebookAccount;
        [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response,NSError * error){
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

            NSLog(@"response = %@", responseString);
        }];

是的我缺少发布流权限但我似乎无法弄清楚我应该把它放在哪里,这整个代码是在用户按下按钮时调用的一种方法

1 个答案:

答案 0 :(得分:0)

很简单。这是我的实施:

- (void)requestAccessToFacebookAccountWithCompletionHandler:(QCFacebookCommunicatorCompletionHandler)completionHandler {
    [[self accountStore] requestAccessToAccountsWithType:[self accountType] options:[self facebookAccountAccessRequestOptionsForReadStreamPermission] completion:^(BOOL accessForReadingGranted, NSError *error) {
        if (accessForReadingGranted) {
            [[self accountStore] requestAccessToAccountsWithType:[self accountType] options:[self facebookAccountAccessRequestOptionsForPublishStreamPermission] completion:^(BOOL accessForWrittingGranted, NSError *error) {
                if (accessForWrittingGranted) {
                    completionHandler(YES, nil);
                } else {
                    completionHandler(NO, [error userInfo]);
                }
            }];
        } else {
            completionHandler(NO, [error userInfo]);
        }
    }];
}

我只是使用辅助方法返回带有选项的字典,其他东西与你的类似。

然后,您可以粘贴负责视频上传的代码而不是completionHandler调用,或者您可以使用此实现,并仅在completionHandler中 success == YES 时上传视频。 / p>

希望它有所帮助!