[Closed] Social Networking Plugin (Facebook and Twitter) live!

Discussion in 'iOS Development' started by prime31, Sep 19, 2010.

  1. macfinch

    macfinch

    New Member

    Joined:
    Aug 24, 2010
    Messages:
    139
    Hey all, I did a small video on how I integrated the prime31 social networking plugin in to our iphone game project.
    It uses an older version of the plug-in but I believe it's still quite similar to the newer plug-in.

    I hope this is useful anyway. It's not the only way to do it, but just how I added it to the game

    Awesome plug-ins prime, keep up the great work :)

    Martin

     
  2. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @macfinch, well done Martin! A great resource for everyone and we get to see little you in the corner the whole time :)
     
  3. macfinch

    macfinch

    New Member

    Joined:
    Aug 24, 2010
    Messages:
    139
    :)
     
  4. Basurman

    Basurman

    New Member

    Joined:
    Sep 1, 2010
    Messages:
    10
    Everything is removed, sorry, I didn't want to violate your EULA. I'll continue to deal with this issue and hope will find a solution. Thanks for your help and advices.
     
  5. zeroz

    zeroz

    New Member

    Joined:
    Feb 3, 2008
    Messages:
    6
    Hi, I'm using this code:
    Code (csharp):
    1.  
    2. void Start () {
    3.         FacebookManager.customRequestReceivedEvent += result =>
    4.         {
    5.             ResultLogger.logObject( result );
    6.         };
    7.     }
    8.    
    9.     void completionHandler( string error, object result )
    10.     {
    11.         if( error != null )
    12.             Debug.LogError( error );
    13.         else
    14.             ResultLogger.logObject( result );
    15.     }
    16.    
    17.     private void shareFacebook()
    18.     {
    19.         FacebookBinding.init( "xxx" );
    20.        
    21.         if(!FacebookBinding.isSessionValid())
    22.         {
    23.             FacebookBinding.loginWithRequestedPermissions( new string[] { "publish_actions", "user_games_activity" } );
    24.         }  
    25.         postMessage();
    26.     }
    27.    
    28.     private void postMessage()
    29.     {
    30.         Facebook.instance.postMessage( "Distance: " + distance, completionHandler );
    31.     }
    32.  
    When I'm testing my share botton in the iPhone, when I allow the facebook application and back to the game, in Xcode I receive a EXC_BAD_EXCEPTION in Facebook.instance.postMessage().

    My Facebook app is configured as a "Native iOS App" in Games category.

    What I'm doing bad?
     
  6. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @zeroz, your code is calling postMessage before login completes. You have to be logged in before you can post a message.
     
  7. zeroz

    zeroz

    New Member

    Joined:
    Feb 3, 2008
    Messages:
    6
    @prime31 when you know if login is complete?
     
  8. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @zeroz, directly from the docs:

    // Fired after a successful login attempt was made
    public static event Action loginSucceededEvent;

    // Fired when an error occurs while logging in
    public static event Action<string> loginFailedEvent;
     
  9. zeroz

    zeroz

    New Member

    Joined:
    Feb 3, 2008
    Messages:
    6
    I have added this and I don't know if is correct because I get errors in the same instruction (Facebook.instance.postMessage):

    Code (csharp):
    1.  
    2. private void shareFacebook()
    3.     {
    4.         FacebookBinding.init( "xxx" );
    5.        
    6.         if(!FacebookBinding.isSessionValid())
    7.         {
    8.             FacebookManager.loginSucceededEvent += OnFacebookLogin;
    9.             FacebookBinding.loginWithRequestedPermissions( new string[] { "publish_actions", "user_games_activity" } );
    10.         }  
    11.         else
    12.             postMessage();
    13.     }
    14.    
    15.     void OnFacebookLogin()
    16.         {
    17.                postMessage();
    18.         }
    19.  
    20.    
    21.     private void postMessage()
    22.     {
    23.         Facebook.instance.postMessage( "Distance: " + distance, completionHandler );
    24.     }
    25.  
     
  10. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @zeroz, that looks about right to me though you may want to make sure you are requesting the proper permissions for posting on a users wall without a dialog.
     
  11. AxelMG

    AxelMG

    Member

    Joined:
    Jul 4, 2012
    Messages:
    7
    Hi,

    I am using the SocialNetwork plugin of Prime31 to integrate Facebook with my game but when I run it I get this message in the log: "SendMessage: object FacebookManager not found!"

    The FacebookManager and FacebookEventListener prefabs are in the Scene... any idea of whats going on?

    Should I place those prefabs in any specific place of the Scene?

    If I run the app from Unity they still in the scene and are active... but no message printed in the Log although I have set a Debug.Log call in the Awake() method of FacebookManager.cs. :(

    Any insight please?

    Its really urgent problem btw.

    Thxs!
     
  12. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @axel, the error clearly indicates that Unity was not able to find the FacebookManager in your scene. Double and triple check that it is in your scene and enabled. The easiest way to be certain is to just include it in your initial loading scene.
     
  13. marusu

    marusu

    New Member

    Joined:
    Jul 25, 2008
    Messages:
    9
    Hello Prime,

    I am getting a 'bad URL' error message when using the following code:

    Code (csharp):
    1.  
    2. string URL = "fql?q=select uid, name, pic_square from user  where is_app_user = 0  and uid in (SELECT uid2 FROM friend WHERE uid1 = me()) order by name asc";
    3. Facebook.instance.graphRequest( URL, HTTPVerb.GET, completionHandler );
    4.  
    this works well when I'm using a call to REST API:

    Code (csharp):
    1.  
    2. var hash = new Hashtable();
    3. hash.Add( "query", "select uid, name, pic_square from user  where is_app_user = 0  and uid in (SELECT uid2 FROM friend WHERE uid1 = me()) order by name asc" );
    4. FacebookBinding.restRequest( "fql.query", "POST", hash );
    5.  
    However, I wanted to make it work with Graph API since REST API will be deprecated.

    The above Graph API string works ok with the Graph API Explorer Tool on the developer pages of Facebook (disregarding the graph.facebook.com prefix), and I can't seem to find any other FQL query reference on this thread.

    Thanks in advance!
     
  14. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @marusu, I can't say for sure of it will fix it but I can say that I do see one major issue with how you are doing the request. You are including the "fql" parameter as part of the URL instead of as a parameter in the parameters Dictionary. It will not get properly escaped doing it like that.
     
  15. william7ba

    william7ba

    New Member

    Joined:
    Jan 28, 2012
    Messages:
    4
    Hi prime,
    Is there an event indicating user really post on his facebook wall after I give the custom dialog?
    dialogCompletedEvent will be triggered even user cancel the dialog.
     
  16. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @will, I believe I already answered your email but I'll reiterate here: the dialogCompletedWithUrlEvent will provide all the information that Facebook provides directly to the Unity side of things.
     
  17. jerotas

    jerotas

    Member

    Joined:
    Sep 4, 2011
    Messages:
    2,779
    Hi, I have code to get the list of friends' scores on Facebook, however I want to to display the profile picture of each friend as well (next to their score). So many games do this. Is there not some sort of built-in method to get this info? If not, any ideas on how this is accomplished?

    Thanks!
     
  18. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @jerotas, see Facebooka documentation for how to fetch a profile image. It is available via their standard Graph API.
     
  19. AxelMG

    AxelMG

    Member

    Joined:
    Jul 4, 2012
    Messages:
    7
    Hi,

    Taking into account this paragraph from Facebook Docs I am guessing if the way in which the SocialNetwork plugging gets the AppAccessToken is safe enough.

    In the SocialNetwork plugin there is a method to get the AppAccessToken that requires the AppSecret as a parameter, but having it in local seems to be compromising the security of the app. So how should I implement it in order to not compromise the security of the app? Do I have to keep it in a server and then request it? wouldn't be easy to get and reproduce that call by someone who download the app for bad purposes anyway?
    There is any way to store it in local and access in a safe way?

    Thanks, and thx also for your fast response the other day, it just helped me to re-check effectively :)
     
    Last edited: Jul 6, 2012
  20. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @axel, if you have a server I would recommend doing all calls that require it from your server just as Facebooks docs recommend.
     
  21. AxelMG

    AxelMG

    Member

    Joined:
    Jul 4, 2012
    Messages:
    7
    Hi,
    thx for your previous recommendation, already done and working.

    But I am getting a new error :(

    If I make a call to retrieve the UserID to the graphAPI from the FacebookEventListener on the method FacebookLogin(), the app crashes on iPhone. This doesn't happen on Android.

    The clearer log info I have been able to find is this:

    The call to retrieve the UserId is that one:

    Code (csharp):
    1. public void LoadUserId()
    2.     {
    3.         Debug.Log("Requesting UserId... current value: " + mUserId);
    4.        
    5.         graphRequest( "me", HTTPVerb.GET, ( error, obj ) =>
    6.         {
    7.             // if we have an error we dont proceed any further
    8.             if( error != null )
    9.             {
    10.                 Debug.LogError("Unable to retrieve UserId");
    11.                 Debug.LogError(error);
    12.                 return;
    13.             }
    14.                        
    15.             // grab the userId and persist it for later use
    16.             var ht = obj as Hashtable;
    17.             mUserId = ht["id"].ToString();
    18.            
    19.             Debug.Log("UserId: "+mUserId);
    20.         });        
    21.     }

    Any insight please?

    I manage to make it work not calling LoadUserId() just after the user has been logged in, but then the calls to the Graph API doesn't work, as they doesn't seems to work just using "me" instead of the UserId. :(

    Maybe I am missing something?
     
  22. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @axel, refer to the demo scene for a working example of fetching a userId.
     
  23. AxelMG

    AxelMG

    Member

    Joined:
    Jul 4, 2012
    Messages:
    7
    I did, actually I copied it :p
    This is how its done on the demo scene:

    Code (csharp):
    1. if( GUI.Button( new Rect( xPos, yPos, width, height ), "Graph Request (me)" ) )
    2.         {
    3.                    
    4.             Facebook.instance.graphRequest( "me", HTTPVerb.GET, ( error, obj ) =>
    5.             {
    6.                 // if we have an error we dont proceed any further
    7.                 if( error != null )
    8.                     return;
    9.                
    10.                 // grab the userId and persist it for later use
    11.                 var ht = obj as Hashtable;
    12.                 userId = ht["id"].ToString();
    13.             });
    14.         }
    The only difference I see is that in the demo its called when pressing the button and in my case its called by code on the loginSucceededEvent.
     
  24. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @axel, try providing a delay so that Facebook can complete anything authentication related on their servers.
     
  25. serioustommy

    serioustommy

    Member

    Joined:
    Jun 12, 2012
    Messages:
    121
    Hi prime,

    I'm trying to use the graph API to post a feed with extra parameter "actions". Here's what it says in the facebook doc:

    "A JSON array containing a single object describing the action link which will appear next to the "Comment" and "Like" link under posts. The contained object must have the keys name and link."

    How do I go about creating such an object? I've tried:

    var parameters = new Dictionary<string,object>
    {
    ...
    { "actions", new ArrayList{new Hashtable{
    {"name", "Play now!"},
    {"link", "http://xxx"}
    }}}
    };

    or

    var parameters = new Dictionary<string,object>
    {
    ...
    { "actions", new Hashtable[]{new Hashtable{
    {"name", "Play now!"},
    {"link", "http://xxx"}
    }}}
    };

    or

    var parameters = new Dictionary<string,object>
    {
    ...
    { "actions", new Dictionary<string, object>[]{new Dictionary<string, object>{
    {"name", "Play now!"},
    {"link", "http://xxx"}
    }}}
    };

    How do I go about constructing a JSON object with arrays and hashtables? I was assuming that I can just use the same type that a returned JSON object uses. Any suggestions?

    thanks!
     
  26. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @serious, I can't say for sure what they are looking for as I do not work for Facebook but it sounds like they want the actions parameter to have something like this:

    [{"name":"the name", "link":"http://link.com"}]
     
  27. serioustommy

    serioustommy

    Member

    Joined:
    Jun 12, 2012
    Messages:
    121
    Ah thanks I didn't know you could just use a raw JSON string as one of the parameter. I thought the plugin re-encodes the parameters into JSON string based on the object type, judging from how Facebook.postImage actually use an array of bytes as the "picture" parameter.

    I'll give it a try now, thanks!
     
  28. TomRab

    TomRab

    Member

    Joined:
    Jul 12, 2012
    Messages:
    2
    Is anyone having issues posting to facebook? I've tried several times today using Facebook.instance.postMessage and nothing is posting to my wall. In the debug log in XCode it says, "Posted to facebook successfully!", but when I go look on my wall there's nothing there. I've checked my facebook app settings as well and everything appears to be fine.
     
  29. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @tom, make sure you dont post the same thing multiple times. Facebook will flag it as a dupe and not display it.
     
  30. TomRab

    TomRab

    Member

    Joined:
    Jul 12, 2012
    Messages:
    2
    I don't think that's the issue, unfortunately, I've made sure not to dupe my posts.

    EDIT: Whoops, just realized the Posted to facebook successfully! message what my own debug log (derp). In the callback from posting a message, if the string in the completion handler is null, does that mean success? Or is that not always the case?

    EDIT2: Looks like it was me just making dumb assumptions!
     
    Last edited: Jul 12, 2012
  31. iRetrograde

    iRetrograde

    Member

    Joined:
    Oct 13, 2011
    Messages:
    93
    Hey Prime, how's it going?

    I've started to develop the backend of my app for multiplayer and I have a server running (.Net MVC server).

    I've already integrated your plugin into my game (Social Networking -> Android) and that worked perfectly. Having said that, I'm guessing that I have to re-direct some data from my game after the authorization is successful, to the server so the server can communicate directly with FB.

    I'm guessing that performing all queries, posts, etc... should occur from the server (as I've read before)?

    I'm wondering if there are any things in particular that I should be looking out for integration wise? I'm guessing that I should still do all of my authorization up-front from within the game (using the plugin) and then send the session data to the server?

    Can I modify the FB server callbacks and whatnot to point to my server without it affecting the plugin?

    Cheers!
     
  32. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @iretro, after successfully logging a user in all you have to do is send the access token to your server. That will enable your server to make any calls that it wants at any time to Facebooks servers.
     
  33. sicklebrick

    sicklebrick

    Member

    Joined:
    Apr 8, 2012
    Messages:
    18
    Hi,cheers for the plugins, they seem to be working amazingly well.. except for one thing.
    I logged in with my main account (the one used to register the app), and posted away fine. I once wrote the connection code more or less from scratch, so I was blown away with the ease of using these. I tried to log in with my test account though, and ran into a peculiar bug...
    I recieved the "An error Occured. Please try later" error in the facebook style with only "Error" at the top.
    Hitting OK or the little X wil dismiss the window fine, but from this point on that's the only screen I can get (i.e. not even a login prompt) whenever any sort of dialog is supposed to pop up. So for example, even if I force close my app, relaunch and hit the Login button, I get that message before any sort of login prompt.
    Clearing the data from the android applications menu lets me log in with my dev account again, as if nothing had ever happened.

    Maybe there's some sort of review process meaning only admins can log into a newly created app (it's less than 2 hours old), but still implies some sort of bug with the plugin.

    I disabled Enhanced Auth as per a suggestion a few pages back, but no joy.
    Any ideas?
     
  34. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @sickle, that error usually occurs due to misconfiguration of your Facebook app. You have to ensure the URL scheme is setup properly, your bundle ID is correct in both Xcode and on Facebooks web portal and that your provisioning profile matches the bundle ID used. Facebook will not auth a user or reopen your app without all of those details in place.
     
  35. iRetrograde

    iRetrograde

    Member

    Joined:
    Oct 13, 2011
    Messages:
    93
    Thanks a ton prime!
     
  36. Crazy Robot

    Crazy Robot

    Member

    Joined:
    Apr 18, 2009
    Messages:
    933
    Hello,

    So I have my app all set up on Facebook except for the:

    "iPhone App Store ID:" and "iPad App Store ID:"

    And my app wont post into FB. So I went on the iTune Connect and used my Apps "AppID", but FB stated that was invalid. So, I'm not sure what I should do here. My app is not live yet.

    I'm not sure what to do here.

    Thanks.
     
  37. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @crazy, posting requires special permission at login. Did you properly request the required permission per Facebook's documentation? You can insert any valid app ID in there while testing. It doesn't have to be the actual game.
     
  38. sicklebrick

    sicklebrick

    Member

    Joined:
    Apr 8, 2012
    Messages:
    18
    Heh, my bad. The app was still in sandbox mode, and the test user wasn't authed for use. Cheers for the speedy response though :)
     
  39. Crazy Robot

    Crazy Robot

    Member

    Joined:
    Apr 18, 2009
    Messages:
    933
    Ok, I'm lost on the FB side of this.

    Here is what i've done:

    setup an app on FB with my appID and bundle ID.

    I initalize my app with the FB app id

    I added the FB app id to the info.list via the Prime31 menu in Unity.

    When I use "FacebookBinding.login", the FB app comes up and I login. But my app would not post to my FB wall.

    So I changed it to:

    Code (csharp):
    1. var permissions : String[];
    2.         permissions [0] = "publish_actions";
    3.         permissions [1] = "user_games_activity";
    4.         FacebookBinding.loginWithRequestedPermissions( permissions );
    now, when I hit my GUI button to login, nothing happens and I don't get a login fail.
     
    Last edited: Jul 13, 2012
  40. xsodus

    xsodus

    New Member

    Joined:
    Jun 5, 2012
    Messages:
    14
    Hi Prime31

    I need to tweet and upload my video with Twitter 3rd Party Service (such as yFrog , TwitVid , Mobypicture , Posterous , Vodpod). But I don't know how to implement it with the Social Network Plugin and Etcetera for Android.
    Do you have any example or tutorial for this?
     
  41. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @crazy, you probably have a cached access token. Always call logout before trying to test different permissions/settings.


    @xsodus, the plugin only covers Twitter and Facebook. It has nothing to do with any other third party services. Consult their documentation to learn how to use them.
     
  42. renardmf

    renardmf

    Member

    Joined:
    Jun 29, 2012
    Messages:
    25
    Hello,

    I just had a quick question. Is it possible for this plugin to post both to a personal page and target a specific Facebook page at the same time? Basically I am looking for a way to track users who post about my game for contests and what not.

    I was also wondering if I could get information like user locale for regional specific contests. I'd imagine it could be done with the opengraph api?

    Thanks for advance!
     
  43. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @renardfm, if it is possible vai the Graph API then the plugin can do it. Facebook's API is absolutely humongous with hunders and hundreds of methods so we cant tell you everything it is capable of. You will need to check their docs to find out what is available.
     
  44. Crazy Robot

    Crazy Robot

    Member

    Joined:
    Apr 18, 2009
    Messages:
    933
    Ok, I've got things working (mostly)

    I'm trying to get the username with this code:

    Code (csharp):
    1. void facebookLogin (){
    2.  
    3.             Facebook.instance.graphRequest( "me", HTTPVerb.GET, ( error, obj ) =>
    4.             {
    5.                 // if we have an error we dont proceed any further
    6.                 if( error != null )
    7.                     return;
    8.                
    9.                 // grab the userId and persist it for later use
    10.                 var ht = obj as Hashtable;
    11.                 FBuserId = ht["id"].ToString();
    12.             });
    13.            
    14.             FBUserIDTxt.Text = FBuserId;
    15.         PlayerPrefs.SetString("FBUN", FBuserId);
    16. }
    it crashes the game when I login. This is a cut and past from the GUI manager script that came with the plugin.

    I also used a wait statment to wait 1.5 sec after login, but it still crashed the game 1.5 seconds after login.

    Not sure what's wrong.

    JL
     
  45. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @crazy, make sure you grab the latest version of the plugin. A very small change was made that sets the access token before the login event is fired. That will prevent any race conditions that occur from events firing in different order.
     
  46. Crazy Robot

    Crazy Robot

    Member

    Joined:
    Apr 18, 2009
    Messages:
    933

    Yup, I have the latest dated 2012-07-10

    I just bought it yesterday!

    JL
     
  47. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @crazy, in that case what is the error you are seeing? Is the access token being set properly (you can check by logging Facebook.instance.accessToken)? You can also enable request debugging by doing Facebook.instance.debugRequests = true
     
  48. Crazy Robot

    Crazy Robot

    Member

    Joined:
    Apr 18, 2009
    Messages:
    933
    Ok, that may be what I'm missing,

    Do I need to call:

    Code (csharp):
    1. var token = FacebookBinding.getAccessToken();
    after FacebookBinding.init

    or

    FacebookBinding.loginWithRequestedPermissions

    ?
     
  49. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @crazy, you shouldnt need to call any of those. The plugin will handle fetching the access token and storing it in the Facebook.instance.accessToken field.
     
  50. BazLevelUp

    BazLevelUp

    New Member

    Joined:
    Jun 27, 2012
    Messages:
    36
    Hello prime31 !

    I have a question : using SocialNetwork for Android, I try to post msg on Facebook wall, but I haven't yet been able to do it.

    1°) I call "FacebookAndroid.login();" which on success callback...
    2°) ... a function in which
    Facebook.instance.postMessageWithLinkAndLinkToImage(
    FACEBOOKMESSAGE,
    FACEBOOKLINK,
    "Get Peti now",
    FACEBOOKIMAGE,
    "",
    null);
    is called and we breaks point in...
    3°) ... postMessageWithLinkAndLinkToImage(etc...)

    but I can't see the message on the wall :\
    What am I doing wrong here please ?