Search Unity

Facebook SDK for Unity StandAlone Builds , Weblayer & WebGL

Discussion in 'Works In Progress - Archive' started by RJproz, Apr 19, 2015.

  1. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    Since two days i m working to build Facebook SDK that would work with Unity Standalone Builds.

    Here is progress :-
    1) Works well with Standalone Builds (Windows,MAC & Linux) [Completed] :D

    2) Also Works with Webplayer Builds. [Working But Some Fixes Needed] :rolleyes:
    But here i m finding a way to Open Login URL in new Tab. Currently its being blocked by "Popup Blocker".

    3) Failed to Work with WebGL [Further Work is Needed]
    Here WWW is not working. :eek:


    You can check the Webplayer Build Here. Make sure you allow popups for this Link. After Login it will show your Facebook Name & will Load your profile picture.

    http://hybriona.com/gamedemos/FBSDK/webplayer/


    EDIT:
    Updated project URL. Get it from Bitbucket.
    Instructions:
    Create a Facebook App.
    1) Go to Settings, and add "Web" platform. Fill the website (where PHP files are hosted).
    2) Add App domains. It must be the same domain or domains where the PHP files are hosted.

    Example below:
    https://www.dropbox.com/s/zt9a5kxxqkbuunf/Screen Shot 2017-04-28 at 8.54.24 PM.png?dl=0
    https://www.dropbox.com/s/sw0ppl3jg37y1c9/Screen Shot 2017-04-28 at 8.55.30 PM.png?dl=0

    3) Call his function to set the URL (again where the PHP files are hosted)
    Code (CSharp):
    1. HybFacebookConstants.SetApiURL("url");
    2. //EG. HybFacebookConstants.SetApiURL("http://www.hybriona.com/services/fbapi/FBProcess.php");
     
    Last edited: May 3, 2017
    IEdge and vijayrathore99 like this.
  2. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
  3. Cheny

    Cheny

    Joined:
    Aug 27, 2013
    Posts:
    5
    Hey RJproz, any progress on Webplayer/WebGL builds? I'm curious to see if this will work :D

    Cya!
     
  4. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    The only solution i found is to put HTML based Login Button. That button can be created manually or by patching html code via ExternalEval. That login button can be "<a href" or javascript "OnClick" which won't be blocked by Browser popup Blockers.
     
  5. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    How is this effort progressing along?
     
  6. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    Login Button, i mean "Opening a link in new tab/popup" is the most tricky part in Web Builds. I didn't get any good solutions for now. So i m using the SDK for Standalone builds only (Window,Mac and Linux).
     
  7. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    I meant for standalone
     
  8. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    It is working fine for standalone builds.
     
  9. Superflat

    Superflat

    Joined:
    Mar 19, 2009
    Posts:
    354
    For some reason when i compile a stand alone player no connect popup appears. Reading the facebook site it says the SDK doesnt support unity standalone builds : / Do you have a link to the sdk you are using?
     
  10. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    I have created that SDK myself. I m requesting https://graph.facebook.com/ using Unity's WWW. And so it is possible to implement almost all features given by official FB SDK, as internally they use the same. But the login process is quite different. We have to redirect the user to FB login page via some web browser (or Webview!!).
     
  11. VMSMedia

    VMSMedia

    Joined:
    Aug 6, 2015
    Posts:
    10
  12. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    That is a good idea! I never thought of releasing it on Asset Store.
    But i can give a blog link from where i started.
    http://www.ifc0nfig.com/accessing-facebook-unity-game-2/

    You need a server (PHP or any other) to get the access token of login process. I have also tested it by creating custom server using TcpListener. The choice would be yours.

    And yes, the main part is just the login process, which is quite different from what we see in Official SDK. The rest, is just simple WWW and WWWForm.
     
    eisenpony likes this.
  13. VMSMedia

    VMSMedia

    Joined:
    Aug 6, 2015
    Posts:
    10
    Thank You for reacting so quickly!
    The link does not work unfortunately.
    It sounds like an interesting method you have there.
    I would love to learn more about it.
     
  14. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    _Yash_ likes this.
  15. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    man, I really wish facebook would just support this out of the box. Any reason why they done?
     
  16. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
  17. p87

    p87

    Joined:
    Jun 6, 2013
    Posts:
    318
    As of yesterday, the Facebook Login is still popup-blocked in WebGL (using chrome). Browsers typically do this because the popup was triggered programatically, and not from within a javascript click event.

    Unless the wizards at facebook can figure something out, in order to avoid the popup blocker you will probably have to implement your own web / javascript based facebook login flow, and integrate that using this feature that unity provides: http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

    One exception I have noticed, is that the Facebook SDK in WebGL will actually work properly (no popup blocker problems) if it's running as a Facebook Canvas game (loaded within facebook). I think this is because it must access the FB javascript SDK within facebook, skips the oauth/login, and goes straight to permissions (which is displayed as a modal within the facebook canvas).

    So I think i'll have to do something special (ala link above) when the WebGL game is not running within the facebook canvas.
     
    Last edited: Dec 14, 2015
  18. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    @philwinkel
    I think i got a solution.

    Webplayer is now deprecated. So, yes WEGL is also my main focus to deploy my games for Web Platform.
    WebGL build also creates a index html file. But here when we submit the Webgl build (to Web Portals or our own), we need to submit the zip. That means the index file will also be there. So we can modify the html file and add login button using 'href'. And then we can link the logic between browser and unity as given in the link you gave.

    :D
     
  19. fred_gds

    fred_gds

    Joined:
    Sep 20, 2012
    Posts:
    184
    I see u got Facebook working on Standalone. I am currently struggling to get the access token as I am not very good with server side scripting so I was wondering whether you could help me out a little ;)
     
  20. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    I see the importance of the project but i m too lazy to create a Asset store package for it. So, i have uploaded the project in BitBucket. Let's just keep it free. (No selling please :D:D )

    https://bitbucket.org/rjprozstandalone/fb_sdk/
     
  21. fred_gds

    fred_gds

    Joined:
    Sep 20, 2012
    Posts:
    184
    Thank you very much!!!!! :)
     
  22. norbertots

    norbertots

    Joined:
    Jul 4, 2014
    Posts:
    15
    i need a solution to send a picture to a facebook fan page in Unity3d standalone or web build in Windows 7? canm yuou give me a example with source code to do this ? please help me..
     
  23. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    Is there anything in our Facebook app settings we need to be aware of? I get an OAuthException, code 191 on my redirect. Right now, in the facebook settings, its pointed to my php code, same as the client's WebsiteURL. Is this correct?
     
  24. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    It is too late so i don't know if it is still relevant or not.
    Login
    Code (CSharp):
    1. HybFacebook.Instance.Init ("app id","app secret","email,user_friends,publish_actions,publish_pages,manage_pages");
    2.         HybFacebook.Instance.Login ();
    3.         HybFacebook.Instance.onLoggedInSuccessfully += delegate() {
    4.          //handle login
    5.         };
    Uploading Image to your own Fan Page
    Code (CSharp):
    1. string pageID = "pade-id";
    2. HybFacebook.Instance.API (pageID+"?fields=access_token", HybFacebook.HTTPMethod.GET, delegate(FacebookResponse response) {
    3.             if(response.error != null)
    4.             {
    5.                 Debug.Log(response.error);
    6.             }
    7.             else
    8.             {
    9.                 Debug.Log(response.text);
    10.                 string token = ((Dictionary<string,object>) Hybriona.MiniJSON.Json.Deserialize(response.text))[HybFacebook.Key_AccessToken].ToString();
    11.                 postToPage(token);
    12.             }
    13.            
    14.         }, null);
    Code of "postToPage" is this -
    Code (CSharp):
    1. WWWForm formData = new WWWForm ();
    2.             formData.AddField ("message", quote);
    3.             formData.AddBinaryData("photo",imgData,"Quote.jpg");
    4.             formData.AddField ("scheduled_publish_time", date.AddHours(-5.5).UnixTimeNow().ToString());
    5.             formData.AddField ("published", "0");
    6.  
    7.             formData.AddField("access_token",page_token);
    8.             HybFacebook.Instance.API (pageID+"/photos", HybFacebook.HTTPMethod.POST, delegate(FacebookResponse response) {
    9.                 if(response.error != null)
    10.                 {
    11.                     Debug.Log(response.error);
    12.                 }
    13.                 else
    14.                 {
    15.  
    16.                     Debug.Log(response.text);
    17.  
    18.                 }
    19.                 Counter++;
    20.                
    21.             }, formData);
     
  25. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    Have you added a Website platform? Just like Android and iOS you need to add a "website" platform from your App settings.
    And make sure to change WebsiteURL in HybFacebookConstants.cs . It should be the url of the php code.

    I don't remember much about FB settings so i m sending you some screenshots
     

    Attached Files:

  26. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    Ya so I have the website platform. The only difference we have is that for me, having Web OAUTH Login toggled on, requires a Valid OAuth redirect URIs. My gut tells me though that''s not the problem since going directly to my php code gives me the 500 error while going directly to your does not.

    Note: A super basic php example does work on my site. It's just the FbLogin.php that gets the 500 error.

    As you can probably tell, I'm a php noob but I have a hunch there's something in FbLogin.php that I need to change. Like autoload.php? Is that required and is there an example of it somewhere?
     
  27. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    Yeah! Unity's WWW doesn't show the actual cause and it logs 500 error.
    There could be a syntax error in php file. May be because you haven't installed the needed library.

    Have you installed phpFastCache via composer?
    And you have do change the 'require_once' path (if you haven't done it)
    If your php server doesn't allow ssh access to install composer then just simply remove that 'require_once' line and do it via old-school method i.e including that library manually.
    https://github.com/PHPSocialNetwork/phpfastcache
     
  28. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    Sweet. Ok so some progress, I've isolated what causes the 500 error.

    I manually included the librabry using
    Code (CSharp):
    1. include '[myUrlLocation]/src/autoload.php';
    Now the 500 error stops when I comment out:
    Code (CSharp):
    1. $cache = phpFastCache();
    2. $cache->set($id ,$data,120 );    
    this is right before the final line:
    Code (CSharp):
    1. header('Location: ?statusok=1');
    I think once I fix this itll be good to go. Any idea? Thank you so much btw you've been incredibly helpful :)
     
  29. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    1. $cache = phpFastCache();
    2. $cache->set($id ,$data,120 );
    is important and actually implements the library.

    I think there is still issue in "include" path and is not pointing towards the right library.

    I m assuming that you have downloaded phpFastCache library from github and uploaded it your server at some place, say '[myUrlLocation]'.


    Then refer this https://github.com/PHPSocialNetwork/phpfastcache/tree/final/examples/legacy_examples
    Here are some examples for non-composer users.


    And for libraries always use require or require_once rather than include or include_once
    include will run the php file even if there are errors in it.

    The include and require statements are identical, except upon failure:

    • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
    • include will only produce a warning (E_WARNING) and the script will continue

    I don't know if you are aware or not (so no offence intended) that the path is specific to the server. For example the path can be
    /home/<user>/public_html/phpfastcache/src/autoload.php

    but definitely not
    /public_html/phpfastcache/src/autoload.php or http://yourwebsite.com/phpfastcache/src/autoload.php

    So confirm if the included path after require or require_once is correct or not. I m saying this because i, myself got stuck in it many times.
     
  30. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    Boom! Got it working. YOU ARE THE ABSOLUTE BEST!

    Thank you so much for your time and effort!
     
  31. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
    One thank you is not enough so I'm double thank posting! :)
     
  32. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    :p:p:p:p
     
  33. tidus4

    tidus4

    Joined:
    Jan 8, 2014
    Posts:
    2
    Hi RJproz,

    I am currently working on a projects where the player will take a screenshots of the gameplay and share it on their Facebook wall. When their share it, the system must prompt user id and password for the facebook then share it on their wall. However, the build will be unity standalone desktop build. So I am having difficulties in finding a right source on the internet as most of the tutorial and forums are about on android build. Since I am working on the standalone build, none of the tutorials are working out for me. Kindly help if you have any sample codes where I can utilized it for my projects. Many thanks in advanced.
     
  34. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    Download my library from Bitbucket.
    One Test script is also present in the library where many simple examples are written including "taking screenshots and uploading it to FB wall".
    Remember that you will need "publish_actions" permission for this. And so you have to send your game for review. Why? Read here.

    For the library to work you will also need PHP hosting, as you need to host 2 files online.
     
  35. sergiosoba

    sergiosoba

    Joined:
    Apr 28, 2017
    Posts:
    8
    Thanks for your work but your demo dont works.

    Login Failed!
    Object reference not set to an instance of an object, at Hybriona.Facebook.HybFacebook+<FetchCode>c__Iterator2.MoveNext () [0x0012f] in C:\Users\Sergio\Desktop\RJprozOfficial-fb-sdk-for-unity-standalone-web-1748ff085c31\Assets\Hybriona\Facebook\HybFacebook.cs:221

    I need to post photos in a specific wall...
    Thanks a lot.
     
  36. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    Open "HybFacebook.cs", browse to line 220. And write the following above it.
    Code (CSharp):
    1. Debug.Log("www.text : "+www.text);
    Send me that text. The demo is working on my machine, so I guess there is something wrong with the server response.
     
  37. sergiosoba

    sergiosoba

    Joined:
    Apr 28, 2017
    Posts:
    8
    Hi again,

    I've been testing your SDK and it worked for my purposes (thank you). I also used the code posted above for publishing on fan pages, but it only works when "published" is turned on; because when I use "scheduled_publish_time" it doesn't. I don't get any error, it just doesn't post anything.

    Thanks for your help.
    Kudos
     
  38. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    It is working. But I think you are scheduling as yourself.
    "Unpublished posts must be posted to a page as the page itself."
    So you need a page access token first. And so "publish_pages" permission is also needed for this.

    Here is one demo.

    Code (CSharp):
    1. #region FBFanPage
    2.     void PostFBFanPageSchedule()
    3.     {
    4.         string pageID = "1575434306036383";
    5.  
    6.  
    7.  
    8.         HybFacebook.Instance.API (pageID+"?fields=access_token", HybFacebook.HTTPMethod.GET, delegate(FacebookResponse response) {
    9.             if(response.error != null)
    10.             {
    11.                 Debug.Log(response.error);
    12.             }
    13.             else
    14.             {
    15.                 Debug.Log(response.text);
    16.                 string page_token = ((Dictionary<string,object>) Hybriona.MiniJSON.Json.Deserialize(response.text))[HybFacebook.Key_AccessToken].ToString();
    17.  
    18.                 System.DateTime date = System.DateTime.Now;
    19.                 date = date.AddHours(1); //Adding one hour for schedule
    20.                 date = date.AddHours(-5.5); // To GMT time. My timezone is GMT + 5hr 30mins, so I subtracted 5.5 hours.
    21.                 WWWForm formData = new WWWForm();
    22.                 formData.AddField("message","But man is not made for defeat. A man can be destroyed but not defeated.\n\n-Ernest Hemingway");
    23.                 formData.AddField("published","false");
    24.                 formData.AddField("scheduled_publish_time",date.UnixTimeNow().ToString());
    25.                 formData.AddField("access_token",page_token);
    26.  
    27.                 HybFacebook.Instance.API(pageID+"/feed",HybFacebook.HTTPMethod.POST,delegate(FacebookResponse response2) {
    28.  
    29.                     Debug.Log(response2.error);
    30.                     Debug.Log(response2.text);
    31.                 },formData);
    32.             }
    33.  
    34.         }, null);
    35.  
     
    Last edited: May 3, 2017
  39. dianadorinamita

    dianadorinamita

    Joined:
    Jan 21, 2016
    Posts:
    25
    i publish 4 games recently on facebook and it past 2 months and not see players on my game, please help
     
  40. uniteiro

    uniteiro

    Joined:
    Mar 11, 2014
    Posts:
    12
    good job man
     
    RJproz likes this.
  41. fabsk9

    fabsk9

    Joined:
    Jul 3, 2012
    Posts:
    14
    very good!
     
    RJproz likes this.
  42. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    11
    Thank you for the Project.Its working on my system.Can u just tell me how u r sending the information from bowser to unity??
     
  43. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    Information is not sent from browser to Unity because I don't know how to achieve it. For Logging/FB Feed callback a simple trick is used. It's more like server to Unity communication.
    1) Unity opens an URL in external default browser (via Application.OpenURL)
    2) WWW call to a server URL with a specific ID. It waits for the response forever or for N seconds
    3) PHP code processes the data sent from step 1 and saves the resultant data using phpFastCache with the same ID as used in step 2. phpFastCache is a temporary caching library for PHP.
    4) Php file on the server (as specified by URL in step 2) now gets the data saved (step 3) and prepares it for HTTP Get request. And So the WWW Call in step 2 will get the data in Unity.
     
    ranch000 likes this.
  44. Eidern

    Eidern

    Joined:
    Mar 29, 2014
    Posts:
    98
    Any thoughts on how to avoid the login to be blocked by pop up blockers on WebGL?
    Maybe being asked to be logged before the game loads in a first page?
     
  45. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    I haven't tried it yet but I think you can use HTML Login button.
    When the user clicks on the button, do the following:-
    1) Open login url in new tab or popup
    2) Send a flag to Unity code that login process has started. Refer this https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
     
  46. Eidern

    Eidern

    Joined:
    Mar 29, 2014
    Posts:
    98
    I'm wondering about the fact to open in popUp, because the main drawback of the unity log in to Fb, is that the Connection popUp is always blocked by default by all webBrowsers. and you'll have to manually enable it - and loss half of your gamers that didn't understand that in the process-
    And I've seen that it happens sometimes in html login as well if you call the Fb.login if nested in async callbacks.
    So I was thinking about how to avoid these login pop ups.. with no clue yet :/
     
  47. RJproz

    RJproz

    Joined:
    May 19, 2013
    Posts:
    52
    It's not a problem with Unity. Javascript popups are blocked by default. That is why I said about HTML Login button.
     
  48. AdhikS

    AdhikS

    Joined:
    Dec 5, 2016
    Posts:
    11
    Thank you RJproz for the proper explanation.
     
  49. Eidern

    Eidern

    Joined:
    Mar 29, 2014
    Posts:
    98
    Hello again, so thanks to you i'm progressing little by little (as i'm a hobbyist, not a pro).
    after struggling a lot to get unity and Js communicate (the docs at https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html are kinda wrong concerning including dll with the new ver I guess, it made tons of compiler error and I'm not the only one to get the same), I did succeed to sendmessage when The user is logged via the JsFbLogin button embbed in the page.

    My Solution so far to avoiding the Fb Login Popup thanks to you, is to embbed the Fb log in Js button on the page, and in the js logged code insert a
    gameInstace.SendMessage() with the user token access in it to Unity
    So far the pop up displays and the user is connected (in a html way)
    I'm now looking to log in unity with my user Access token ..
    I hope it won't last long :/
     
    Last edited: Jul 25, 2017
  50. Eidern

    Eidern

    Joined:
    Mar 29, 2014
    Posts:
    98
    Well the only solution so far is to integrate Fb directly as a web App canvas : it's only working this way. If my app is started from facebook it's okay, but outside Facebook you'll get the pop up block.