在将视频发布到Facebook时,必须使用活动访问令牌来查询有关当前用户错误的信息

时间:2014-02-10 03:33:24

标签: ios facebook social-framework

这是我将视频发布到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);
                }];
            }
        }];
    }

}];

但它的回归:

  

“OAuth \”Facebook平台\“\”invalid_request \“\”必须使用有效的访问令牌来查询有关当前用户的信息。\“”;

我不知道我错过了什么。有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

您忘记将facebook帐户设置为请求。 获得访问权限后,请获取帐户:

                           NSArray *accounts = [self.accountStore
                                accountsWithAccountType:fbAccountType];
                        self.facebookAccount = [accounts lastObject];

并在创建请求后设置帐户

    [request setAccount:self.facebookAccount];

答案 1 :(得分:0)

发现我必须将令牌添加到params字段:

NSString *status = @"One step closer.";
NSDictionary *params = @{@"title":status, @"description":status, @"access_token":accessToken};

希望它可以帮助他人。