Posting on Facebook and Twitter using prime31 social plugin

Hi, I’ve recently purchased prime31 social networking plugin and tried adding a Facebook and a Twitter button into a game.
Facebook button should just post a pre-defined message on the user’s wall, and Twitter should post a message on user’s behalf in his Twitter feed. The player is rewarded by either of the actions.

Below is all of the relevant code I added.

Facebook basically works, BUT if the user is initially not logged in, then the loginSucceededEvent doesn’t come, so the user has to press the Facebook button once again to actually post (both on Android and iOS).

Also, on Android, if the user tries to post on Facebook right after loggin in (second press on Facebook button), he would always get

java.io.FileNotFoundException: https://graph.facebook.com/me/feed

After restarting the game it posts ok.

Twitter has similar problem: it posts, but basically no events ever fire.

On Android twitterInitializedEvent is never fired. According to prime31 - Mobile game and app experts specializing in Unity games and plugins, I placed “http://www.google.com” into “Callback URL:” on twitter developer page for the app, but this didn’t change anything.

logcat shows:
I/Prime31 ( 2017): initializing Twitter service
I/Prime31-OAS( 2017): request token endpoint: https://api.twitter.com/oauth/request_token
and nothing else.

On iOS showOauthLoginDialog successfully opens the Twitter login page and TwitterBinding.postStatusUpdate creates the tweet, but
neither of these fire:
TwitterManager.loginSucceededEvent
TwitterManager.postSucceededEvent
TwitterManager.postFailedEvent

I guess there may just be something wrong with the way I am attaching delegates?

private void InitializeFacebook()
	{
		Debug.Log("InitializeFacebook");
		FacebookAccess.init(FACEBOOK_APP_ID);
		FacebookManager.loginSucceededEvent += OnFacebookLoggedIn;
		FacebookManager.customRequestReceivedEvent += OnCustomFacebookEvent;
	}
	
	private void InitializeTwitter()
	{
		Debug.Log("InitializeTwitter");
#if UNITY_ANDROID
		TwitterAndroidManager.twitterInitializedEvent += OnTwitterInitialized;
		TwitterAndroid.init(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
		TwitterAndroidManager.loginDidSucceedEvent += OnTwitterLoginAndroid;
#elif UNITY_IPHONE
		TwitterBinding.init(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
		TwitterManager.loginSucceededEvent += OnTwitterLoginIOS;
		TwitterManager.postSucceededEvent += OnPostedOnTwitter;
		TwitterManager.postFailedEvent += OnPostOnTwitterFailed;
#endif
	}

	private void CleanUpFacebook()
	{
		Debug.Log("CleanUpFacebook");
		FacebookManager.loginSucceededEvent -= OnFacebookLoggedIn;
		FacebookManager.customRequestReceivedEvent -= OnCustomFacebookEvent;
	}

	private void CleanUpTwitter()
	{
		Debug.Log("CleanUpTwitter");
#if UNITY_ANDROID
		TwitterAndroidManager.twitterInitializedEvent -= OnTwitterInitialized;
		TwitterAndroidManager.loginDidSucceedEvent -= OnTwitterLoginAndroid;
#elif UNITY_IPHONE
		TwitterManager.loginSucceededEvent -= OnTwitterLoginIOS;
		TwitterManager.postSucceededEvent -= OnPostedOnTwitter;
		TwitterManager.postFailedEvent -= OnPostOnTwitterFailed;
#endif
	}
	
	public void OnFacebookLoggedIn()
	{
		Debug.Log("OnFacebookLoggedIn");
		PostOnFacebook();
	}
	
	private void PostOnFacebook()
	{
		Debug.Log("PostOnFacebook");
#if UNITY_ANDROID
		Facebook.instance.postMessageWithLinkAndLinkToImage(
				"I'm playing this game. Check it out", 
				"https://play.google.com/store/apps/details?id=com.nuclearfox.rotation", 
				"Steam Puzzle", 
				"http://www.frostball.com/games_screens/Rotation_100_100.png",
				"GooglePlay",
				OnPostOnFacebookCompletionHandler);
#elif UNITY_IPHONE
		Facebook.instance.postMessageWithLinkAndLinkToImage(
				"I'm playing this game. Check it out", 
				"http://itunes.apple.com/us/app/rotation/id528259248", 
				"Steam Puzzle", 
				"http://www.frostball.com/games_screens/Rotation_100_100.png",
				"AppStore",
				OnPostOnFacebookCompletionHandler);
#else
		this.sPopupMessage = "Facebook integration is only implemented for Android and iOS";
#endif
		
	}
	
	public void OnPostOnFacebookCompletionHandler(string sError, object data)
	{
		if (null != data)
			ResultLogger.logObject(data);
		if (null != sError)
		{
			Debug.LogError(sError);
			this.sPopupMessage = "Error while connecting to Facebook. \nPlease retry later. \n\nError: " + sError + "... ";
			return;
		}
		PlayerPrefs.SetInt("has_posted_on_facebook", 1);
		
		this.sPopupMessage = "Thanks for telling your friends about the game!";
	}
	
	public void OnCustomFacebookEvent(object obj)
	{
		if (null != obj)
			ResultLogger.logObject(obj);
	}
	
	private bool bIsTwitterInitialized = false;
	public void OnTwitterInitialized()
	{
		Debug.Log("OnTwitterInitialized");
		bIsTwitterInitialized = true;
	}
	public void OnTwitterLoginAndroid(string sUserScreenName)
	{
		Debug.Log("OnTwitterLoginAndroid: " + sUserScreenName);
		PostOnTwitter();
	}
	public void OnTwitterLoginIOS()
	{
		Debug.Log("OnTwitterLoginIOS");
		PostOnTwitter();
	}
	
	private void PostOnTwitter()
	{
		Debug.Log("PostOnTwitter");
#if UNITY_ANDROID
		//Application.CaptureScreenshot("screenshot.png");
		//string pathToImage = Application.persistentDataPath + "/" + "screenshot.png";
		//byte[] bytes = System.IO.File.ReadAllBytes(pathToImage);
		//TwitterAndroid.postUpdateWithImage("test update from Unity!", bytes);
		TwitterAndroid.postUpdate("I am currently playing Steam Puzzle. Check it out: https://play.google.com/store/apps/details?id=com.nuclearfox.rotation");
		// since on Android no event is fired when message is posted on Twitter,
		// immediately assume that the user has posted successfully
		PlayerPrefs.SetInt("has_posted_on_twitter", 1);
#elif UNITY_IPHONE
		TwitterBinding.postStatusUpdate("I am currently playing Steam Puzzle. Check it out: http://itunes.apple.com/us/app/rotation/id528259248");
		// on iOS an event will fire when message is posted on Twitter
#endif
	}
	
	public void OnPostedOnTwitter()
	{
		Debug.Log("OnPostedOnTwitter");
		PlayerPrefs.SetInt("has_posted_on_twitter", 1);
	}
	
	public void OnPostOnTwitterFailed(string sError)
	{
		Debug.Log("OnPostOnTwitterFailed");
		this.sPopupMessage = "Failed to post on Twitter! \nPlease retry later\n\nError: " + sError;
	}
	
	void OnGUI()
	{
		GUI.skin = this.guiSkin;
		
		// if a popup is shown, draw it in front of everything
		if (null != this.sPopupMessage)
		{
			GUI.DrawTexture(this.rectDialog, this.texBgDialog);
			Game.DrawLabel(this.rectDlgText, this.sPopupMessage, this.guiSkin);
			if (GUI.Button(this.rectDlgOK, "", this.guiSkin.customStyles[CustomStyle.BTN_OK]))
			{
				if (Game.isSoundOn)
					audio.PlayOneShot(this.audioButtonPress);
				this.sPopupMessage = null;
			}
			return;
		}
		
		if (GUI.Button(this.rectFacebookBtn, "", this.guiSkin.customStyles[CustomStyle.BTN_FACEBOOK]))
		{
			if (Game.isSoundOn)
				audio.PlayOneShot(this.audioButtonPress);
#if UNITY_ANDROID || UNITY_IPHONE
			// post immediately if already logged in, otherwise log in
			if (!FacebookAccess.isSessionValid())
			{
				Debug.Log("User not logged into Facebook, attempting login");
  				FacebookAccess.loginWithRequestedPermissions( new string [] {"publish_stream", "publish_actions"} );
			}
			else
			{
				PostOnFacebook();
			}
#else
			this.sPopupMessage = "Facebook integration is only implemented for Android and iOS";
#endif
		}
		if (GUI.Button(this.rectTwitterBtn, "", this.guiSkin.customStyles[CustomStyle.BTN_TWITTER]))	
		{
			if (Game.isSoundOn)
				audio.PlayOneShot(this.audioButtonPress);
#if UNITY_ANDROID
			if (!this.bIsTwitterInitialized)
			{
				this.sPopupMessage = "Twitter not initialized!";
				return;
			}
			if (!TwitterAndroid.isLoggedIn())
			{
				Debug.Log("User not logged into Twitter, attempting login");
				TwitterAndroid.showLoginDialog();
			}
			else
			{
				PostOnTwitter();
			}
#elif UNITY_IPHONE
			if (!TwitterBinding.isLoggedIn())
			{
				Debug.Log("User not logged into Twitter, attempting login");
				TwitterBinding.showOauthLoginDialog();
			}
			else
			{
				PostOnTwitter();
			}
#else
			this.sPopupMessage = "Twitter integration is only implemented for Android and iOS";
#endif
		}
..
}

I am calling
InitializeFacebook() and InitializeTwitter() in Start()
and CleanUpFacebook() and CleanUpTwitter() just before calling Application.Quit()

Thanks!

Put TwitterAndroidManager and TwitterManager prefabs on the scene.

Thanks, this solved the problem!

Hi,

First thanks because you gave me the solution for my Facebook connection problem !

For Twitter, the postEvent delegates work well in our code. I hope it helps you.

/// <summary>
	///  Init Twitter Login or Post if already logged
	/// </summary>
	void InitTwitter() {
		Debug.Log("InitTwitter(): init");
		TwitterBinding.init(twitterConsumerKey, twitterConsumerSecret);
		
		if(!TwitterBinding.isLoggedIn()) {
			// si l'utilisateur n'est pas encore loggue sur Twitter
			TwitterLogin();
		}
		else {
			// si l'utilisateur est deja loggue sur Twitter
			TwitterPost(Language.Get("SHARE_TWITTER","Sketcher"));
		}
	}
	
	
	/// <summary>
	/// Login to Twitter
	/// </summary>
	void TwitterLogin() {
		Debug.Log("TwitterLogin(): login to account");
		// on cree les event handlers
		TwitterManager.loginSucceededEvent += OnTwitterLogin;
		TwitterManager.loginFailedEvent += OnTwitterLoginFailed;
		
		// on affiche la fenetre d'authentification Twitter
		TwitterBinding.showOauthLoginDialog();
	}


	/// <summary>
	/// Appele lorsque l'utilisateur s'est loggue avec succes
	/// </summary>
	void OnTwitterLogin() {
		Debug.Log("OnTwitterLogin(): login successful");
		// on desinscrit les fonctions
		TwitterManager.loginSucceededEvent -= OnTwitterLogin;
		TwitterManager.loginFailedEvent -= OnTwitterLoginFailed;
		
		
		TwitterPost(Language.Get("SHARE_TWITTER","Sketcher"));
	}


	/// <summary>
	/// Appele lorsque l'utilisateur n'a pu se logguer
	/// </summary>
	void OnTwitterLoginFailed(string _error) {
		Debug.LogError("OnTwitterLoginFailed(): error = "+_error);
		// on desactive les event handlers
		TwitterManager.loginSucceededEvent -= OnTwitterLogin;
		TwitterManager.loginFailedEvent -= OnTwitterLoginFailed;
		
		// on affiche un message d'erreur
		ShowMessage("Failed to login to Twitter.");
	}


	/// <summary>
	/// Envoie le post
	/// </summary>
	/// <param name="_message">
	/// A <see cref="System.String"/>
	/// </param>
	void TwitterPost(string _message) {
		Debug.Log("TwitterPost("+_message+")");
		// on cree les event handlers
		TwitterManager.postSucceededEvent += OnTwitterPost;
		TwitterManager.postFailedEvent += OnTwitterPostFailed;
		// on affiche la barre de chargement
		EtceteraBinding.showBezelActivityViewWithLabel(Language.Get("TWITTER_POST","Sketcher"));

		// on poste le message
		TwitterBinding.postStatusUpdate(_message,screenshotLogoPath);
	}

	/// <summary>
	/// Appele lorsque le post a ete publie
	/// </summary>
	void OnTwitterPost() {
		Debug.Log("OnTwitterPost(): post published");
		// on desactive les event handlers
		TwitterManager.postSucceededEvent -= OnTwitterPost;
		TwitterManager.postFailedEvent -= OnTwitterPostFailed;
		// on cache la barre de chargement
		EtceteraBinding.hideActivityView();
		
		// on ecrit qu'on a bien effectue un partage par twitter (sert pour decrementer ou non le compteura la fin)
		bTwitterShared = true;
		// on affiche un message
		ShowMessage(Language.Get("TWITTER_DONE","Sketcher"));
	}

	/// <summary>
	/// Appele lorsqu'une erreur est survenue pendant le post
	/// </summary>
	void OnTwitterPostFailed(string _error) {
		Debug.LogError("OnTwitterPostFailed(): error = "+_error);
		// on desactive les event handlers
		TwitterManager.postSucceededEvent -= OnTwitterPost;
		TwitterManager.postFailedEvent -= OnTwitterPostFailed;
		// on cache la barre de chargement
		EtceteraBinding.hideActivityView();
		
		// on affiche un message d'erreur
		ShowMessage("Failed to send your post to Twitter.\n("+_error+")");
	}

to me something similar happens in social media prime31 ios works fine but on android or test esena ada I served and I re3gresa access console 401 error token, anyone can help me