Unity Fabric Compose Tweets in Android

android
unity

#1

Issue: Uri file path resolved to null

I’m trying to use the Fabric SDK in Unity to compose a tweet. I am able to login no problems but once my app tries to compose a tweet nothing happens, here’s a snippet of the code (I replaced the actual google play id):

public void LogInComplete(TwitterSession session)
{
    string imgPath = Application.streamingAssetsPath + "/banner.png";
    Card card = new AppCardBuilder()
        .ImageUri(imgPath)
        .GooglePlayId("xxxx");

    Twitter.Compose(session, card);
    
}

I placed the banner.png in my unity streamingassets folder, but I run the app on my android phone I get this error in logcat:

com.twitter.sdk.android.core.TwitterException: Uri file path resolved to null

I have been looking everywhere for an example where a person uses Unity and manages to get a correct URI that gets a successful tweet composed…


#2

Hi,
Haven’t tested it but you might want to try:
string imgPath = "file://" + Application.streamingAssetsPath + "/banner.png";

Or you can try upgrading to Twitter Kit 3.0 for Unity (however app cards were removed).

regards,
Adam


#3

I switched to Twitter Kit 3.0 and now my app crashes upon loading, I see the logcat on my android phone and it says:

Unable to instantiate application io.fabric.unity.android.FabricApplication: java.lang.ClassNotFoundException: Didn’t find class “io.fabric.unity.android.FabricApplication”

I followed the instructions, I imported the asset and I put in my api and secret key…


#4

After importing the assets you need to update your import statements and remove all references to Fabric (assuming you aren’t using any other Fabric products).


#5

I removed and reimported all my plugins and the crash is gone. I still can’t successfully tweet… I tried many path names, even the one you suggested: string imgPath = “file://” + Application.streamingAssetsPath + “/banner.png”;

I see in the demo provided you use the persistentDataPath directory so I tried that and even that doesn’t work this is how I changed my code (but I can now see the image inside the tweet composer):

public void LogInComplete(TwitterSession session)
{
    Application.CaptureScreenshot("Screenshot.png");
    string imgPath = "file://" + Application.persistentDataPath + "/Screenshot.png";

    string[] hashtags = { "#xxx", "#xxx" };
    string msg = "...";
    Twitter.Compose(session, imgPath, msg, hashtags);

}

Interestingly I got a different error when I tried the screenshot version:

E/Twitter: Unexpected response
java.lang.NullPointerException: Attempt to invoke interface method ‘boolean java.util.List.isEmpty()’ on a null object reference

07-19 15:13:58.518 6480-6480/? E/TweetUploadService: Post Tweet failed
com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 401


#6

Have you tried loading the demo scene alone? Try to compose a tweet with screenshot from the demo. Leaving compose unmodified it should look like:

public void LoginCompleteWithCompose(TwitterSession session) {
		Application.CaptureScreenshot("Screenshot.png");
		UnityEngine.Debug.Log ("Screenshot location=" + Application.persistentDataPath + "/Screenshot.png");
		string imageUri = "file://" + Application.persistentDataPath + "/Screenshot.png";
		
		Twitter.Compose (session, imageUri, "Welcome to", new string[]{"#TwitterKitUnity"});
	}

#7

I get the same error, it’s a 401 unauthorized response, what level permissions do I need? My current application settings on twitter is read/write, isn’t that good enough to compose a tweet?

Here’s the full error stack:

07-19 20:03:13.966 8475-8475/? E/Twitter: Unexpected response
java.lang.NullPointerException: Attempt to invoke interface method ‘boolean java.util.List.isEmpty()’ on a null object reference
at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
07-19 20:03:13.969 8475-8475/? E/TweetUploadService: Post Tweet failed
com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 401
at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)


#8

Try creating a brand new app (https://apps.twitter.com/) and use the api keys from there to test. Leave all the default settings and set a dummy callback url. Let me know if that works so we can determine if it’s your app or something else.


#9

Still didn’t work with the same error (401). I didn’t generate an access token, that doesn’t matter right?


#10

I would also prefer to use streamingassets instead of using the screenshot, I find the screenshot inconsistent in behavior and in fact apparently it is obsolete according to unity docs: https://docs.unity3d.com/ScriptReference/Application.CaptureScreenshot.html


#11

I managed to copy my banner.png from streaming assets to persistent data path so now I use:

string imgPath = “file://” + Application.persistentDataPath + “/banner.png”;

I can see the banner image inside the tweet composer but again I’m getting a 401 error response


#12

Please test composing from the demo scene with your new test app’s api keys. Don’t try modifying the screenshot yet.


#13

It worked and I tried my original application on apps.twitter.com and it works too… I added the email request permission, callback url, privacy policy url and terms of service url. Thanks for the help


Unity iOS, Android composer callback
#14

Everything works now and you can compose a tweet with your banner.png?


#15

Yes I can compose with banner.png, thanks!


#16