如何使用iOS sdk发表评论

时间:2013-05-15 13:17:33

标签: ios facebook-ios-sdk facebook-comments facebook-graph-api

我需要使用iOS Facebook SDK评论图形对象,我不能这样做。评论正在使用社交插件,但我不想使用UIWebView。我怎样才能使它有效?

-(void)postAction
{
    if ([textField.text isEqualToString:@""]) return;
    if ([[FBSession activeSession] isOpen])
    {
        if ([FBSession.activeSession.permissions indexOfObject:@"publish_stream"] == NSNotFound)
        {
            [[FBSession activeSession] requestNewPublishPermissions:@[@"publish_stream"] defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) {
                [self getFBObjectGraphID];
            }];
        } else {
            [self getFBObjectGraphID];
        }
    } else {
        [FBSession openActiveSessionWithPublishPermissions:@[@"publish_stream"] defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
            [self getFBObjectGraphID];
        }];
    }
}

-(void)getFBObjectGraphID
{
    NSString *debateURL = @"OPEN_GRAPH_OBJECT_URL";
    NSString *query =
    @"SELECT comments_fbid FROM link_stat WHERE url = \"%@\"";
    query = [NSString stringWithFormat:query, debateURL];
    // Set up the query parameter
    NSDictionary *queryParam =
    [NSDictionary dictionaryWithObjectsAndKeys:query, @"q", nil];
    // Make the API request that uses FQL
    [FBRequestConnection startWithGraphPath:@"/fql"
                                 parameters:queryParam
                                 HTTPMethod:@"GET"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error) {
                              if (error) {
                                  NSLog(@"Error: %@", [error localizedDescription]);
                              } else {
                                  NSLog(@"Result: %@", result);
                                  NSNumber *stringID = [[[result objectForKey:@"data"] objectAtIndex:0] objectForKey:@"comments_fbid"];
                                  if (stringID)
                                      [self postCommentWithOpenGraphID:[stringID stringValue]];
                              }
                          }];
}

-(void)postCommentWithOpenGraphID:(NSString*)ogId
{
    NSMutableDictionary *params =
        [NSMutableDictionary dictionaryWithObjectsAndKeys:
            textField.text, @"message",
            [FBSession activeSession].accessTokenData.accessToken, @"access_token",
         nil];
    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/comments",ogId]
                                 parameters:params HTTPMethod:@"POST"
        completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            if (error)
            {
                NSLog(@"error: %@", error.localizedDescription);
            }
            else
            {
                NSLog(@"result: %@",result);
            }
    }];

}

这是我的代码中的样子。我已经将代码更新为我的实际代码。现在我首先从FQL获取Open Graph Object ID,然后进行注释操作。如果我将POST更改为GET,我收到了所有评论,因此ID可以。如果我发表评论我得到错误:

Printing description of error:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x1d16a590 {com.facebook.sdk:HTTPStatusCode=500, com.facebook.sdk:ParsedJSONResponseKey={
    body =     {
        error =         {
            code = 1;
            message = "An unknown error has occurred.";
            type = OAuthException;
        };
    };
    code = 500;
}


permissions:(
    "video_upload",
    "share_item",
    "publish_stream",
    installed,
    "status_update",
    email,
    "create_note",
    "photo_upload",
    "publish_actions"
)

0 个答案:

没有答案