void Start() { // dump custom data to log after a request completes FacebookManager.graphRequestCompletedEvent += result => { Prime31.Utils.logObject(result); }; // when the session opens or a reauth occurs we check the permissions to see if we can publish FacebookManager.sessionOpenedEvent += () => { _hasPublishActions = FacebookBinding.getSessionPermissions().Contains("publish_actions"); }; FacebookManager.reauthorizationSucceededEvent += () => { _hasPublishActions = FacebookBinding.getSessionPermissions().Contains("publish_actions"); }; // grab a screenshot for later use Application.CaptureScreenshot(screenshotFilename); // this is iOS 6 only! _canUserUseFacebookComposer = FacebookBinding.canUserUseFacebookComposer(); // optionally enable logging of all requests that go through the Facebook class //Facebook.instance.debugRequests = true; }
void sessionOpenedEvent() { // do we need publish permissions? if (requiresPublishPermissions && !FacebookBinding.getSessionPermissions().Contains("publish_actions")) { FacebookBinding.reauthorizeWithPublishPermissions(new string[] { "publish_actions" }, FacebookSessionDefaultAudience.Everyone); } else { afterAuthAction(); cleanup(); } }
public void start() { FacebookBinding.login(); }
// Sets the app version that Facebook will use for all events public static void setAppVersion(string version) { FB.setAppVersion(version); }
// Shows the native Facebook Share Dialog. Valid dictionary keys (from FBShareDialogParams) are: link, name, caption, description, picture, friends (array) public static void showFacebookShareDialog(Dictionary <string, object> parameters) { FB.showFacebookShareDialog(parameters); }
// Full access to any existing or new Facebook dialogs that get added. See Facebooks documentation for parameters and dialog types public static void showDialog(string dialogType, Dictionary <string, string> options) { FB.showDialog(dialogType, options); }
// Logs the user out and invalidates the token public static void logout() { FB.logout(); }
// Logs an event, valueToSum and optional parameters public static void logEvent(string eventName, double valueToSum, Dictionary <string, object> parameters = null) { FB.logEvent(eventName, valueToSum, parameters); }
// Checks to see if the current session is valid public static bool isSessionValid() { return(FB.isSessionValid()); }
// Reauthorizes with the requested publish permissions and audience public static void reauthorizeWithPublishPermissions(string[] permissions, FacebookSessionDefaultAudience defaultAudience) { FB.reauthorizeWithPublishPermissions(permissions, defaultAudience); }
// Reauthorizes with the requested read permissions public static void reauthorizeWithReadPermissions(string[] permissions) { FB.reauthorizeWithReadPermissions(permissions); }
// Authenticates the user for the provided permissions public static void loginWithReadPermissions(string[] permissions) { FB.loginWithReadPermissions(permissions); }
// Gets the url used to launch the application. If no url was used returns string.Empty public static string getAppLaunchUrl() { return(FB.getAppLaunchUrl()); }
// Prepares the Facebook plugin for use public static void init() { FB.init(); }
// Gets the current access token public static string getAccessToken() { return(FB.getAccessToken()); }
private void secondColumnButtonsGUI() { // only show posting actions if we have permission to do them if (_hasPublishActions) { if (GUILayout.Button("Post Image")) { var pathToImage = Application.persistentDataPath + "/" + FacebookGUIManager.screenshotFilename; #if UNITY_EDITOR pathToImage = Application.dataPath.Replace("Assets", screenshotFilename); #endif if (!System.IO.File.Exists(pathToImage)) { Debug.LogError("there is no screenshot available at path: " + pathToImage); return; } var bytes = System.IO.File.ReadAllBytes(pathToImage); Facebook.instance.postImage(bytes, "im an image posted from iOS", completionHandler); } if (GUILayout.Button("Post Message")) { Facebook.instance.postMessage("im posting this from Unity: " + Time.deltaTime, completionHandler); } if (GUILayout.Button("Post Message & Extras")) { Facebook.instance.postMessageWithLinkAndLinkToImage("link post from Unity: " + Time.deltaTime, "http://prime31.com", "Prime31 Studios", "http://prime31.com/assets/images/prime31logo.png", "Prime31 Logo", completionHandler); } } else { GUILayout.Label("Reauthorize with publish_actions permissions to show posting buttons"); } if (GUILayout.Button("Graph Request (me)")) { Facebook.instance.getMe((error, result) => { // if we have an error we dont proceed any further if (error != null) { return; } if (result == null) { return; } // grab the userId and persist it for later use _userId = result.id; Debug.Log("me Graph Request finished: "); Debug.Log(result); }); } if (GUILayout.Button("Show stream.publish Dialog")) { // parameters are optional. See Facebook's documentation for all the dialogs and parameters that they support var parameters = new Dictionary <string, string> { { "link", "http://prime31.com" }, { "name", "link name goes here" }, { "picture", "http://prime31.com/assets/images/prime31logo.png" }, { "caption", "the caption for the image is here" } }; FacebookBinding.showDialog("stream.publish", parameters); } if (GUILayout.Button("Show apprequests Dialog")) { // see Facebook's documentation for all the dialogs and parameters that they support var parameters = new Dictionary <string, string> { { "title", "This Is The Title" }, { "message", "message goes here" } }; FacebookBinding.showDialog("apprequests", parameters); } if (GUILayout.Button("Get Friends")) { Facebook.instance.getFriends(completionHandler); } if (_canUserUseFacebookComposer) { if (GUILayout.Button("Show Facebook Composer")) { // ensure the image exists before attempting to add it! var pathToImage = Application.persistentDataPath + "/" + FacebookGUIManager.screenshotFilename; if (!System.IO.File.Exists(pathToImage)) { pathToImage = null; } FacebookBinding.showFacebookComposer("I'm posting this from Unity with a fancy image: " + Time.deltaTime, pathToImage, "http://prime31.com"); } } if (GUILayout.Button("Show Facebook Share Dialog")) { var parameters = new Dictionary <string, object> { { "link", "http://prime31.com" }, { "name", "link name goes here" }, { "picture", "http://prime31.com/assets/images/prime31logo.png" }, { "caption", "the caption for the image is here" }, { "description", "description of what this share dialog is all about" } }; FacebookBinding.showFacebookShareDialog(parameters); } }
// Gets the permissions granted to the current access token public static List <object> getSessionPermissions() { return(FB.getSessionPermissions()); }
void OnGUI() { // center labels GUI.skin.label.alignment = TextAnchor.MiddleCenter; beginColumn(); if (GUILayout.Button("Initialize Facebook")) { FacebookBinding.init(); } if (GUILayout.Button("Login")) { // Note: requesting publish permissions here will result in a crash. Only read permissions are permitted. var permissions = new string[] { "email" }; FacebookBinding.loginWithReadPermissions(permissions); } if (GUILayout.Button("Reauth with Publish Permissions")) { var permissions = new string[] { "publish_actions" }; FacebookBinding.reauthorizeWithPublishPermissions(permissions, FacebookSessionDefaultAudience.OnlyMe); } if (GUILayout.Button("Enable Frictionless Requests")) { FacebookBinding.enableFrictionlessRequests(); } if (GUILayout.Button("Logout")) { FacebookBinding.logout(); } if (GUILayout.Button("Is Session Valid?")) { bool isLoggedIn = FacebookBinding.isSessionValid(); Debug.Log("Facebook is session valid: " + isLoggedIn); Facebook.instance.checkSessionValidityOnServer(isValid => { Debug.Log("checked session validity on server: " + isValid); }); } if (GUILayout.Button("Get Access Token")) { var token = FacebookBinding.getAccessToken(); Debug.Log("access token: " + token); } if (GUILayout.Button("Get Granted Permissions")) { // This way of getting permissions uses the Facebook SDK. It is not always accurate since it does not hit the FB servers var permissions = FacebookBinding.getSessionPermissions(); foreach (var perm in permissions) { Debug.Log(perm); } _hasPublishActions = permissions.Contains("publish_actions"); // This way of getting the permissions hits Facebook's servers so it is certain to be valid. Facebook.instance.getSessionPermissionsOnServer((error, grantedPermissions) => { // check for a successful call then register if the publish_actions permissions is present if (grantedPermissions != null) { _hasPublishActions = grantedPermissions.Contains("publish_actions"); } }); } if (GUILayout.Button("Log App Event")) { var parameters = new Dictionary <string, object> { { "someKey", 55 }, { "anotherKey", "string value" } }; FacebookBinding.logEvent("fb_mobile_add_to_cart", parameters); } endColumn(true); // toggle to show two different sets of buttons if (toggleButtonState("Show OG Buttons")) { secondColumnButtonsGUI(); } else { secondColumnAdditionalButtonsGUI(); } toggleButton("Show OG Buttons", "Toggle Buttons"); endColumn(false); if (bottomRightButton("Twitter...")) { Application.LoadLevel("TwitterTestScene"); } }
// Logs an event with optional parameters public static void logEvent(string eventName, Dictionary <string, object> parameters = null) { FB.logEvent(eventName, parameters); }