Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by helloAndre · May 26, 2017 at 12:47 PM · c#facebooksdkapigraph

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image helloAndre · May 26, 2017 at 07:14 PM 0
Share

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 convert Facebook.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'*

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

356 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Facebook SDK - Scores Paging and Pagination 0 Answers

How to post as Page using the Facebook SDK 1 Answer

MultipartFormDataSection not working - Error: 403 0 Answers

Facebook for Windows 1 Answer

[ANDROID] Get appUsers with Facebook SDK in development build 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges