Get comments on Unity using Facebook Graph API
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;
using Facebook.Unity;
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;
void Awake () {
FB.Init (SetInit);
}
void SetInit()
{
if (FB.IsLoggedIn) {
Debug.Log ("FB is logged in");
} else {
Debug.Log ("FB is not logged in");
}
DealWithFBMenues (FB.IsLoggedIn);
}
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: https://youtu.be/U0FTq_QQRyI
I feel like I'm making some progress with this, however I'm still getting errors:
void DisplayPostID (IResult result)
{
var dict = Json.Deserialize(result) as Dictionary<string,object>;
//string userName = dict ["name"];
var PostID = DialogPostID.GetComponent<Text> ();
var comments = new List<object> ();
string commentUser;
if (dict.TryGetValue ("comments", out PostID)) {
comments = (List<object>)(((Dictionary<string, object>)PostID) ["data"]);
if (comments.Count > 0) {
var commentDict = ((Dictionary<string,object>)(comments [0]));
var comment = new Dictionary<string, string> ();
comment ["id"] = (string)commentDict ["id"];
comment ["name"] = (string)commentDict ["name"];
}
//get_data.text = result.ResultDictionary ["first_name"] + " is Logged In";
} else {
Debug.Log (result.Error);
}
Errors:
error CS1502: The best overloaded method match for
Facebook.$$anonymous$$iniJSON.Json.Deserialize(string)' has some invalid arguments - error CS1503: Argument
#1' cannot convertFacebook.Unity.IResult' expression to type
string'Assets/Scripts/FBScript.cs(163,59): error CS0030: Cannot convert type
UnityEngine.UI.Text' to
System.Collections.Generic.Dictionary'*