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 omar-padierna · Apr 08, 2017 at 11:10 PM · iosfacebookcallback

FB.API Callback issue

Hello, I am using Unity 5.5.2f1 pro and facebook's SDK v 7.9.4

I have a script which after login (managed in a previous scene) sends an API request to FB asking for friends, name and email and sends that info as a POST to a php website.

note: Since this is meant for iOS I have to test it on a device so I'm using text in the screen to display info (think of it as a badly implemented print statement).

The problem: In my callback function for FB.API I write in the text Gameobject (aka testTxt) the parsed information from the response which is saved in the Custom UserInfo clss. It display's correctly but the code gets stuck there. It doesn't continue to the next function. HOWEVER, if I delete/comment that line and don't display anything in the text field. The codes does continue to the POST function BUT the information from the API call is not passed, i.e my custom class is empty (leading me to believe the callback function is not called at all).

Im stuck here any help is appreciated. Thanks!

 [Serializable]
 public struct FBData {
     public string first_name;
     public string email;
     public string friends;
     public string id;
 
 }
 
 public class UserManagement : MonoBehaviour {
 
     string urlSaveUserData="some php website";
     public Text testTxt;
     FBData parsedData;
 
     // Use this for initialization
     void Start () {
         //Check if it's the first time the user is opening the app. 
         if (UserInfo.FIRST_TIME) {
             //update text (only used for testing, should be removed in production.) 
             testTxt.text = "Your user id is: " + UserInfo.ID;
             //Perform FB.API call to get User Data.
             getUserData ();
 
             //Save in SQL table. (won't get here if line in getUserData() is active)
             StartCoroutine ("saveUserData");
         } else {
             //do something else.
         }
 
 public void getUserData(){
 
         string query = "me?fields=first_name,email,friends";
         FB.API (query, HttpMethod.GET, Apicallback, new Dictionary<string, string> ());
 
     }
 
     private void Apicallback(IGraphResult result){
         //Parse Graph response into a specific class created for this result. 
         parsedData = JsonUtility.FromJson<FBData>(result.RawResult);
 
         //Pass each field into UserInfo class. 
         UserInfo.EMAIL = parsedData.email;
         UserInfo.FRIENDS = parsedData.friends;
         UserInfo.NAME = parsedData.first_name;
         UserInfo.FACEBOOKID = parsedData.id;
 
                 /*problem area, if I comment line below, then previous information is apparently not stored. If left as is then testTxt displays correct information but code gets stuck there.  */
         testTxt.text = "This is the info from USerInfoInside the APICallback: " + UserInfo.EMAIL + UserInfo.FRIENDS + UserInfo.FACEBOOKID;
     }
 
 public IEnumerator saveUserData() {
         //get user info (this information is EMPTY if line in getUserData() is commented. 
         parsedData.id = UserInfo.FACEBOOKID;
         parsedData.friends = UserInfo.FRIENDS;
         parsedData.first_name = UserInfo.NAME;
         parsedData.email = UserInfo.EMAIL;
 
         //translate data into json
 
         string JsonBodyData = JsonUtility.ToJson (parsedData);
 
         //MAke custom  web request (POST method doesnt seem to work very well, documentation example sends empty form)
         var w = new UnityWebRequest(urlSaveUserData, "POST");
         byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(JsonBodyData);
         w.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
         w.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
         w.SetRequestHeader("Content-Type", "application/json");
 
         yield return w.Send();
 
 //work with received data...}
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by amoswazana · Jun 25, 2017 at 08:18 AM

It seems that you are not waiting for the graph API callback before trying to parse the user info, thus the user information will be empty. Without changing too much of your code you can just move the StartCoroutine("saveUserData") call to the end of the graph API callback.

Comment
Add comment · Share
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

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

117 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

Related Questions

Facebook SDK - iOS - Doesn't return user data 1 Answer

Facebook sharing on iOS with Unity game 0 Answers

Facebook SDK Login crashes on iOS 10 2 Answers

Facebook SDK - Authorize Window pops up, user already authorized. 1 Answer

Unity Facebook SDK v7.9.0 totally doesn't work on iOS (Unity version 5.3.2) 1 Answer


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