1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Get comments on Unity using Facebook Graph API

Discussion in 'Facebook API' started by helloAndreLeon, May 23, 2017.

  1. #1
    I'm trying to extract Facebook comments from a Facebook Live video using Facebook Graph API (Explorer). I managed to login in and extract data from my user like my name and profile picture.

    I successfully extracted single line data like the name of the post and time it was posted, but I'm struggling with getting list data like comments, likes or reactions.


    When attempting to get list data the following text is displayed:
    "System.Collections.Generic.Dictionary`2[System.String,System.Object]"

    The error displays:
    "KeyNotFoundException: The given key was not present in the dictionary."

    Here is my code:

    using System.Collections;
    using UnityEngine;
    using UnityEngine.UI;

    using System.Collections.Generic; //to use dictionaries and lists
    using Facebook.Unity; //to use unity functions

    public class FBScript : MonoBehaviour {
    public GameObject DialogLoggedIn;
    public GameObject DialogLoggedOut;
    public GameObject DialogUsername;
    public GameObject DialogProfilePic;

    public GameObject DialogPostID;
    public GameObject DialogPostUsername;
    public GameObject DialogPostComment;

    // Update is called once per frame
    void Awake () {
    FB.Init (SetInit/*, OnHideUnity*/);
    }

    void SetInit()
    {
    if (FB.IsLoggedIn) {
    Debug.Log ("FB is logged in");
    } else {
    Debug.Log ("FB is not logged in");
    }
    DealWithFBMenues (FB.IsLoggedIn);
    }

    //---LOGGING IN---
    //--------------------
    public void FBlogin()
    {
    List<string> permissions = new List<string> ();
    permissions.Add ("public_profile");
    permissions.Add ("user_about_me");
    permissions.Add ("user_friends");
    permissions.Add ("manage_pages");
    permissions.Add ("user_photos");
    permissions.Add ("user_posts");
    FB.LogInWithReadPermissions (permissions, AuthCallBack);
    }
    void AuthCallBack(IResult result)
    {
    if (result.Error != null) {
    Debug.Log (result.Error);
    } else {
    if (FB.IsLoggedIn) {
    Debug.Log ("FB is logged in");
    } else {
    Debug.Log ("FB is not logged in");
    }
    DealWithFBMenues (FB.IsLoggedIn);
    }
    }
    void DealWithFBMenues(bool isLoggedIn)
    {
    if (isLoggedIn) {
    DialogLoggedIn.SetActive (true);
    DialogLoggedOut.SetActive (false);
    FB.API("/me/?fields=first_name", HttpMethod.GET,DisplayUsername);
    FB.API("/me/picture?type=square&height=150&width=150", HttpMethod.GET,DisplayProfilePic);
    } else {
    DialogLoggedIn.SetActive (false);
    DialogLoggedOut.SetActive (true);
    }
    }

    //---REQUESTING DATA---
    //----------------------------
    public void FBRequestData()
    {
    FB.API("/632374026862851_938491859584398/?fields=comments{id}", HttpMethod.GET,DisplayPostID);
    FB.API("/632374026862851_938491859584398/?fields=comments{from}", HttpMethod.GET,DisplayPostUsername);
    FB.API("/632374026862851_938491859584398/?fields=comments{message}", HttpMethod.GET,DisplayPostComment);
    }

    //---LOGIN DATA---
    //--------------------
    void DisplayUsername (IResult result)
    {
    Text UserName = DialogUsername.GetComponent<Text> ();
    if(result.Error == null) {
    UserName.text = result.ResultDictionary ["first_name"] + " is Logged In";
    } else {
    Debug.Log (result.Error);
    }
    }
    void DisplayProfilePic (IGraphResult result)
    {
    if (result.Texture != null) {
    Image ProfilePic = DialogProfilePic.GetComponent<Image> ();
    ProfilePic.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 150, 150), new Vector2 ());
    } else {
    Debug.Log (result.Error);
    }
    }

    //---POST DATA---
    //-------------------
    void DisplayPostID (IGraphResult result)
    {
    Text PostID = DialogPostID.GetComponent<Text> ();
    if(result.Error == null) {
    PostID.text = result.ResultDictionary ["comments"] + " :post ID";
    } else {
    Debug.Log (result.Error);
    }
    }
    void DisplayPostUsername (IGraphResult result)
    {
    Text PostUsername = DialogPostUsername.GetComponent<Text> ();
    if(result.Error == null) {
    PostUsername.text = result.ResultDictionary ["from"] + " :post Username";
    } else {
    Debug.Log (result.Error);
    }
    }
    void DisplayPostComment (IGraphResult result)
    {
    Text PostComment = DialogPostComment.GetComponent<Text> ();
    if(result.Error == null) {
    PostComment.text = result.ResultDictionary ["message"] + " :post Comment";
    } else {
    Debug.Log (result.Error);
    }
    }
    }

    Here's a link to what's happening:
     
    helloAndreLeon, May 23, 2017 IP
  2. helloAndreLeon

    helloAndreLeon Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #2
    I get the feeling it's something to do with this, but I still can't get to grips with it (I'm a very beginner coder): https://developers.facebook.com/docs/unity/reference/current/Json
     
    helloAndreLeon, May 23, 2017 IP