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

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

  1. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @baz, are you logging the user in with the permissions requires to post on their behalf? You have no completion handler set in your code. There is no way for you to know what is happening without a completion handler which is used to relay information about the request to you.
     
  2. renardmf

    renardmf

    Member

    Joined:
    Jun 29, 2012
    Messages:
    25
    Hey,

    Just one more question though. On your website it shows a file called Facebook.cs which includes a function called postScore() which presumably would post a user score on the app's wall. This file appears to be missing in my version of the plugin. Is the newer version required to make these functions work? or is it possible to still utilize these functions in the older version? I only ask this since the implementation of the older version is already done so upgrading to the newer system wouldn't really be an option. Basically though I am looking for a way to get similar functionality by having my game be able to self post and communicate with a shared community wall.

    Thanks again for your help!
     
  3. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @renardmf, you can call any Graph API method at all with any version of the plugin. The postScore method (or literally any of the other methods in the Facebook class) is nothing more than a wrapper to call the Graph API. For some reason, many folks can't seem to call Graph API methods themselves (or maybe they just don't like to read through Facebook's documentation for the parameters) so we made a little class that has the most common ones.
     
  4. BazLevelUp

    BazLevelUp

    New Member

    Joined:
    Jun 27, 2012
    Messages:
    36
    You were right, I wasn't using a completionHandler (and this is bad, children!). Now that I have used one, I found out 2 things :
    - the callback for logging in successfully is called 4 times oO
    - I got an error string saying "java.io.FileNotFoundException: https://graph.facebook.com/me/feed"

    Edit : IT WORKS ! :)
    Ok I have search a bit on the net about what you told me (I haven't implemented the code myself but I'm the one working with it after so I'm kinda unfamiliar with the plugin :\ ) and I have found that I need to specify the permissions I want in the login.
    In FacebookAndroid.cs, it goes like
    login()
    { loginWithRequestedPermissions( new string[] { "publish_stream" } ); }

    It seems strange to me because the person in the company that worked on that part before me seemed to be able to post without that permissions asking oO
    So anyway it works, woohoo !

    Still, my debugger detect 4 login_successfull callbacks, 2 failing and 2 succeeding at posting messages. But Im' not going to be that scrupulous, now it works, I can go on ! :)
     
    Last edited: Jul 18, 2012
  5. renardmf

    renardmf

    Member

    Joined:
    Jun 29, 2012
    Messages:
    25
    Hello again,

    I managed to update my plugin to the latest version and am able to use new Facebook.cs functions such as postScore. I am however running into an issue when I try to run that particular function from my game.

    Code (csharp):
    1. InvalidCastException: Cannot cast from source type to destination type.
    2.   at Facebook+<postScore>c__AnonStorey1.<>m__1 (System.String error, System.Object obj) [0x00000] in <filename unknown>:0
    3.   at Prime31.P31RestKit.processResponse (UnityEngine.WWW www, System.Action`2 onComplete) [0x00000] in <filename unknown>:0
    4.   at Prime31.P31RestKit+<send>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0
    5.  
    6. (Filename:  Line: -1)
    I thought it might have been something I am doing wrong but even with a new Unity project and a fresh install of the plugin with my app linked in I am still getting this error in the demo scenes. All the other functions seem to work except for this.

    Any ideas of what could be the issue? Or something I could check with my Facebook App setup?

    Thanks again for your help.
     
  6. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @renard, the only things I can think of are that you didn't setup your Facebook papas a game or you didn't request the required permissions. You can enable debugging of requests (which occassionally has a bit of extra info for cases like this where Facebook doesn't send a legit response) by just setting Facebook.instance.debugRequests to true.
     
  7. junksnakes

    junksnakes

    New Member

    Joined:
    Jul 19, 2012
    Messages:
    2
    Does the plugin work with the simulator? I know in an older thread you said it didn't, but that was over a year ago. Just wondering if that's changed.

    Thanks
     
  8. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @junk, Unity does not support plugins in the simulator.
     
  9. renardmf

    renardmf

    Member

    Joined:
    Jun 29, 2012
    Messages:
    25
    Great. It seems like I'm getting a more precise response now:

    Code (csharp):
    1. response text: {"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}}
    I should already have this token if I am able to post to my wall right? I noticed that in Facebook.cs the postScore function uses the appAccessToken twice is that an issue?

    Code (csharp):
    1.         // post the score to the proper path
    2.         var path = userId + "/scores";
    3.         var parameters = new Dictionary<string,object>()
    4.         {
    5.             { "score", score.ToString() },
    6.             { "app_access_token", appAccessToken },
    7.             { "access_token", appAccessToken }
    8.         };
    9.        
     
  10. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @ren, score posts use the app access token and the userId. They don't use normal access tokens per Facebooks docs.
     
  11. renardmf

    renardmf

    Member

    Joined:
    Jun 29, 2012
    Messages:
    25
    OK, well as I mentioned before I am using the demo scenes provided. So, I logged in with the permissions in the demo and graph requested "me", got the app access token, and then attempted to post score all through the GUI and I get that response. My app is also setup as a game. That should work right?
     
  12. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @ren, that same process works fine with all of our test Facebook accounts. you may want to open a bug report with Facebook so they can check their server logs.
     
  13. renardmf

    renardmf

    Member

    Joined:
    Jun 29, 2012
    Messages:
    25
    OK, will do. Thanks for the help.

    The only other thing I saw that was kind of strange is that for the App Access Token I get this in response :

    Code (csharp):
    1. app access token retrieved: 283151685087055|Gku-jC9foAAbCXEOpO2igjhuKgg
    Is it an issue that there is both the App Id and the piped Access Code in my request?
     
  14. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @ren, it appears that is how Facebook makes their app access tokens. The full return string is access_token=283151685087055|Gku-jC9foAAbCXEOpO2igjhuKgg
     
  15. BazLevelUp

    BazLevelUp

    New Member

    Joined:
    Jun 27, 2012
    Messages:
    36
    I seem to have the same issue as samavan did.

    I set up my app to accept "Native iOS App", I set "com.mycompany.mygame" for the Bundle ID, I set nothing for appstore because our game is not out yet, and in "URL Scheme Suffix" I set "fbMYAPPLEID", the same string I put in the editor.

    Still, I'm getting "Safari cannot open the page because the address is invalid"
    I tried to delete the app on the iPhone and reinstall it but I got the same error.

    Do you have any idea please ? :confused:
     
  16. Ale Farinella

    Ale Farinella

    New Member

    Joined:
    Jul 7, 2012
    Messages:
    7
    @Prime, I am working with the social plugin which works really well. I have been able to do almost everything i need. Now i need your advice on this: I am posting screenshot from unity to facebook, logging in, getting auth ... everything fine. But if i remove the apps authorization from facebook and i am still logged in, i post image again, getting the token, i assume is the old one, i post a new images and in the completionhandler i do not get "error" but "result" with an error inside that look like this:

    error: error_subcode: 458
    message: Error validating access token: User 14............95 has not authorized application 20..........14.
    type: OAuthException
    code: 190

    if i would have receive "error" i would have been able to magage it but in "result" i was especting to get also a positive answer to complete the postimage function.

    So, how can i know if i am getting an error in result and not in error ?

    Thks
    Alessandro
     
  17. serioustommy

    serioustommy

    Member

    Joined:
    Jun 12, 2012
    Messages:
    121
    I've been trying to get postScore to facebook to work but couldn't. I searched around on google and found this open issue on facebook:
    http://developers.facebook.com/bugs/461900043821056

    In short, score posting from native apps on iphone won't work because it means the app has the app's secret key compiled in the binary (in order to get the app's session key, not to be confused with the user's session key), and facebook won't allow API calls from such apps.

    The only workaround I can think of would be to host a simple php script on some server, and the app will then make a request to the php script which then posts the score.
     
  18. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @baz, URL scheme suffix should be left empty unless you are an advanced user and know how to use it.


    @ale, it looks like it is invalid JSON for some reason which means it won't get passed through to the completion handler properly. You may want to open a biu with Facebook so that they are aware they are not sending JSON back.


    @serious, we highly recommend having a server to either post the scores or just return the app secret so that it is always safe and never stored on the device.
     
  19. serioustommy

    serioustommy

    Member

    Joined:
    Jun 12, 2012
    Messages:
    121
    I've just udpated the social networking plugin to the latest (7-10-2012), and found out that open graph calls don't work anymore after logging in.

    Upon further investigation, it seems like in FacebookManager.fetchAccessTokenAndSaveToFacebookClass(), the line facebookAccessor = Type.GetType("FacebookBinding") always returns null when run on iphone for some reason. I had to modify it to typeof(FacebookBinding) to get it to work.

    Anything wrong with using reflection on iphone?
     
  20. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @serious, that is quite odd. It seems to fetch the type on our tests without issue. I can't think of any reason that would fail. The only type of reflection that is not permitted on mobile is emit.
     
  21. serioustommy

    serioustommy

    Member

    Joined:
    Jun 12, 2012
    Messages:
    121
    I did some more tests, this time with the plugin's test scenes, and it's the same result.

    It'll only not work if you try to use facebook call that requires access token in the same session where you've logged in. It'll work in the following restart of the game since the access token is stored and is retrieved via FacebookBinding.init() directly and not through reflection in the event callback.

    So to reproduce this:
    - delete existing app to ensure that access token is not present.
    - upload app with social networking test scene to iphone
    - press facebook init
    - press facebook login
    - go to second page and press "Post Message" which posts message through open graph
    - notice in the console it says that "An active access token must be used to query information about the current user"
    - however if you force quit the app (double click home button and remove app from task bar)
    - relaunch the app, click facebook init, then second page "Post Message" again, it'll post this time
     
  22. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @serious, on a fresh install with the latest version of the plugin following your steps exactly the post succeeds on the 2 devices I tested on. Are you sure you have the latest version of the plugin? You can always debug the current access token as well by logging Facebook.instance.accessToken.
     
  23. serioustommy

    serioustommy

    Member

    Joined:
    Jun 12, 2012
    Messages:
    121
    So I suspected that the class might have been stripped out somehow (which I didn't think was the case because the class is referenced elsewhere). Since I couldn't think of anything else I started playing with the player settings in unity.

    And then it worked if I set "script call optimization" to "slow and safe" instead of "fast but no exception" :/

    p.s. I did make double sure that I have the latest plugin by redownloading it again.
     
    Last edited: Jul 23, 2012
  24. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @serious, that is even more odd. We do all testing with script execution set to "fast". Can you try adding a log to the FacebookManager class where it sets the access token to make sure it is actually occurring? I can't imagine Unity would strip that code but you never know.
     
  25. serioustommy

    serioustommy

    Member

    Joined:
    Jun 12, 2012
    Messages:
    121
    Yeah I actually did put log after every line in FacebookManager.fetchAccessTokenAndSaveToFacebookClass(), that's how I discovered facebookBookAccessor is null after the line facebookAccessor = Type.GetType("FacebookBinding"). And when it's null the access token won't get set.

    I also always compile/deploy/test twice just to make sure that I didn't mess anything up.

    Oh well for now I'll probably just leave it as typeof(FacebookBinding) since reflection is just for people who don't own the ios plugin. Just have to remember to reapply all the little changes I've made to the plugins here and there next time I update :)

    Thanks for the help so far.
     
  26. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @serious, we'll keep trying to repro the reflected type being null that you are seeing. For some reason it always works on our test devices...
     
  27. iRetrograde

    iRetrograde

    Member

    Joined:
    Oct 13, 2011
    Messages:
    93
    I seem to be having the same problem as loock_, I'm just about to install the latest version of unity and upgrade my plugin to make sure it's nothing weird on that end. Force internet permission is set.

    Did you guys end up figuring this out? It seems weird that this started happening considering my other project is fine and this project is a derivative of that one.
     
  28. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @iRetro, we have not been able to reproduce the issue. In the future, please keep Android questions in the Android forum.
     
  29. renardmf

    renardmf

    Member

    Joined:
    Jun 29, 2012
    Messages:
    25
    Thanks serioustommy. That seems to be the issue I am running into as well.

    So, Prime, I guess that means I would need to setup a web server that will handle posting the scores since Facebook doesn't support api calls using app tokens on Native/Desktop apps? And potentially a different facebook web app entirely since I would still need the Native/Desktop app to maintain the personal wall posts functionality?
     
  30. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @renardmf, it doesn't mean you need separate apps setup on Facebook. It just means you need a server to perform the requests from.
     
  31. Jon at Kaio

    Jon at Kaio

    Member

    Joined:
    Oct 17, 2007
    Messages:
    153
    Am I doing something wrong here...
    All I'm getting is Bad url when I manually switch back to my game from the facebook app that gets opened by calling the login
     
  32. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @jon, that means you either did not setup your bundle ID, provisioning profile or Facebook app up correctly on their website.
     
  33. Jon at Kaio

    Jon at Kaio

    Member

    Joined:
    Oct 17, 2007
    Messages:
    153
    Im thinking that but I was sure I had, eh hang on
    what needs to happen on the provisioning profile other than just the normal stuff?
     
  34. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @jon, your bundle ID, provisioning profile and Facebook app setup all must match or Facebook will not authorize users.
     
  35. Jon at Kaio

    Jon at Kaio

    Member

    Joined:
    Oct 17, 2007
    Messages:
    153
    mmm okay well they all seem to match as far as I can tell (I wasn't using a specific app provisioning profile before but now am)
    but alas no change.
     
  36. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @jon, if Facebook still wont auth users via your app and you are 100% certain everything is setup correctly you will need to open a bug report with them to find out why.
     
  37. Jon at Kaio

    Jon at Kaio

    Member

    Joined:
    Oct 17, 2007
    Messages:
    153
    Got it logging in fine now, just need to figure out why I'm still getting the 'Bad Url' error messages (even on successful login)
    FB certainly makes it one of the harder plugins to work with.
     
  38. Jon at Kaio

    Jon at Kaio

    Member

    Joined:
    Oct 17, 2007
    Messages:
    153
    Whoops it was because I was posting straight after logging made it look like it was erroring on login when it was the posting that errored.
     
  39. BazLevelUp

    BazLevelUp

    New Member

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

    Well, I still struggle to get my post on Facebook :(
    For now I'm on iOS and I have set my Facebook App like this : link
    I have erased the URL-scheme as you told me I didn't need it to post on wall :
    I'm getting "Safari cannot open the page because the address is invalid"
    What could I be doing wrong?
    The bundle id does exist but the app is not out yet, maybe it's the cause?
     
  40. manaschew

    manaschew

    New Member

    Joined:
    Jul 31, 2012
    Messages:
    1
    Hello prime31.

    I have some problems about FacebookBinding.logout() method.
    When I call it and then call loginWithRequestedPermissions(), the plugin use the previous account to login automatically.

    From this post, http://forum.unity3d.com/threads/60769-Social-Networking-Plugin-%28Facebook-and-Twitter%29-live!/page23 I found that the problem cannot be solved, but it quite an old post so I want to ask again.

    If the answer still the same, is there a way to clear or delete or invalidate the current access token from the plugin cache?
    Now, if I open Facebook official app and logout. the access token still be valid and can use for posting.

    ps. Sorry for my poor English. ^^
     
  41. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @baz, double and triple check your appId, bundle ID, provisioning profile and all the other setup from Facebooks docs.


    @manaschew, there is no way to have credentials not cached by iOS. iOS devices are single user so they cache information as such. The logout method destroys the access token locally but it must still expire on Facebooks servers before it is fully unusable.
     
  42. priznut

    priznut

    Member

    Joined:
    Apr 19, 2012
    Messages:
    16
    Hello,

    I have both the Twitter and the Facebook plugin working great for our game. Thank you for that.

    My question is this, and I know looking into the graph API might give me some answers. Is it possible to post to another users page or wall? We would like to have users post to our developer page to certain albums preferably.
     
  43. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @priznut, it sounds like it should be possible. You will want to check the Graph API docs to be certain though.
     
  44. priznut

    priznut

    Member

    Joined:
    Apr 19, 2012
    Messages:
    16
    Quick follow up question. When posting an image to an album. I noticed you are missing "/photos" at the end of the albumID. The Facebooks docs list that as a requirement to post images. Is that correct?

    Secondly, when using the postImage( string pathToImage, string caption, string albumId ), is that some where being hardcoded to point to the current user on your end?

    We are having no errors appear when we post at the correct Album ID for our app page on Facebook. But we see no results. We tested posting messages and so on using their developer tools, and those work. But for some reason using postImage to specific albums is not working. Yet postImage to a user's wall works perfectly.
     
  45. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @priznut, none of the methods in the Facebook class are any type of magic. They are all plain old Graph requests. The postPhoto method follows Facebooks documentation guideline for posting an image to the mobile uploads album. You will have to consult Facebooks documentation for the required permissions and parameters for the request.
     
  46. Pip Robbins

    Pip Robbins

    Member

    Joined:
    Oct 19, 2010
    Messages:
    213
    I have a random issue.

    Everything was working fine, then one day I start crashing each time I build on launch. The problem seems to be with FacebookManager.mm

    Instance method '-handleOpenURL:' not found (return type defaults to 'id')

    FacebookManager.mm
    Code (csharp):
    1.  
    2. - (void)applicationDidFinishLaunching:(NSNotification*)note
    3. {
    4.     // did we get launched with a userInfo dict?
    5.     if( note.userInfo )
    6.     {
    7.         NSURL *url = [note.userInfo objectForKey:UIApplicationLaunchOptionsURLKey];
    8.         if( url )
    9.         {
    10.             NSLog( @"recovered URL from jettisoned app. going to attempt login" );
    11.             [self handleOpenURL:url];
    12.         }
    13.     }
    14. }
    Failed to load AOT module 'mscorlib' while running in aot-only mode.

    I have rebuilt, deleted app from device, deleted app from device via xcode organiser, reimported plugin, restarted both Mac and iOS device, deleted xcode builds etc

    Any ideas?
     
  47. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @pip, everytime I have seen that error doing a fresh build in Unity then doing a Build->Clean in Xcode has cleared it up.
     
  48. gadgetoflife

    gadgetoflife

    Member

    Joined:
    Aug 11, 2012
    Messages:
    2
    I am using the plugin version iOS 1.4.3. with unity 3.5.3 and export to xcode 4.4.1. The twitter part works fine. But the Facebook park is not working well.

    1. The Login button switch to facebook app and does not return to the unity app. In the unity app i have to login again using the web interface.
    2. Once login, i could post with the "custom feed dialog", but other such as "post message" and "post image" is not working.
    3. Only one time the "Post image" is working but only post black image on facebook.

    Please help.
     
  49. prime31

    prime31

    Member

    Joined:
    Oct 9, 2008
    Messages:
    6,424
    @gadget, 1. If Facebook is not reopening your app you either didn't properly setup your Facebook app or you didn't properly setup your URL scheme.

    2. Refer to Facebooks documentation for the required permissions for each method. You have to request the permissions you need at login.

    3. postImage will post the exact image you pass to the method. Make sure you vary the caption and use a legit image and check the completion handler for the error message Facebook returns to you
     
  50. Pip Robbins

    Pip Robbins

    Member

    Joined:
    Oct 19, 2010
    Messages:
    213
    Thanks Prime31,

    This seems to have resolved the crashing, but I need to clean it every time I build as it keeps coming back. Any idea why?