如何使用FB Graph API获取类似于total_count的facebook?

时间:2016-01-28 06:51:01

标签: ios objective-c facebook-graph-api

我正在编写将facebook整合到我的应用中的代码。我想得到一个对象的总数,我在https://developers.facebook.com/docs/graph-api/reference/v2.5/object/likes阅读了文档 他们说请求是:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                           initWithGraphPath:@"/{object-id}/likes"
                                  parameters:params
                                  HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                  id result,
                                  NSError *error) {
// Handle the result
}];

但是我需要通过哪些参数?

我确实通过了@{@"fields":@"share"}的参数,然后返回的结果是

"share": {
    "comment_count": 0,
    "share_count": 0
  }

我使用带有该URL的Graph API Explorer然后返回:

 {
  "og_object": {
    "id": "952460714815635",
    "type": "website",
    "updated_time": "2015-08-20T08:45:43+0000",
    "url": "http://dev-vn.magestore.com/simicart/1800/index.php/blackberry-8100-pearl.html/likes"
  },
  "share": {
    "comment_count": 0,
    "share_count": 0
  },
  "id": "http://dev-vn.magestore.com/simicart/1800/index.php/blackberry-8100-pearl.html/likes"
}

但是我找不到这个对象的计数。他们提到的total_count属性在哪里?任何有相关经验的人,请帮助我。非常感谢!

4 个答案:

答案 0 :(得分:0)

尝试在nil部分发送parameters。所以你的请求看起来像是:

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                           initWithGraphPath:@"/{object-id}/likes"
                                  parameters:nil
                                  HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                  id result,
                                  NSError *error) {
// Handle the result
}];

在回复中,您应该获得一个键total_count,其中包含相应的总喜欢值。

如果这不起作用,请尝试在["summary" = true]部分发送parameters,并检查您是否在回复中收到了必填项。在这种情况下,您的请求应如下所示:

NSDictionary *setFlag = @[@"summary" = true];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                               initWithGraphPath:@"/{object-id}/likes"
                                      parameters:setFlag
                                      HTTPMethod:@"GET"];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {
    // Handle the result
    }];

请尝试一下,看看它是否有效。

答案 1 :(得分:0)

您可以直接将参数写入路径。我无法测试它,但它应该工作。

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                           initWithGraphPath:@"/{object-id}/likes?summary=true&fields=total_count"
                                  parameters:nil
                                  HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                  id result,
                                  NSError *error) {
// Handle the result
}];

答案 2 :(得分:0)

谢谢你,使用FBSDKGraphRequest似乎不行。我使用NSURLConnection直接通过URL请求并且它已经工作。

 NSString* requestURL = [NSString stringWithFormat:@"https://graph.facebook.com/fql?q=SELECT like_count FROM link_stat WHERE url = \"%@\"",productURL];

NSURL* url = [NSURL URLWithString:[requestURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSDictionary* data = [NSJSONSerialization JSONObjectWithData:responseData
                                                     options:kNilOptions
                                                       error:&error];
[productMoreVC stopLoadingData];

if(!error)
{
    lblLikeCount.text = [NSString stringWithFormat:@"%@", [[[data objectForKey:@"data"] objectAtIndex:0] objectForKey:@"like_count"]];
}

答案 3 :(得分:0)

Facebook上有一个问题。我创建了一个关于这个问题的门票 - 现在是fixed

请求:

/v2.6/me?fields=likes.limit(0)。总结(真)

回复:

{
  "likes": {
    "data": [
    ],
    "summary": {
      "total_count": 7
    }
  },
  "id": "1234567890"
}