1

I just develop android plugin for unity and i am successfully able to exchange data between unity and android plugin.

in my android plugin my code is

String SCOPE = "https://www.googleapis.com/auth/youtube";
am.getAuthToken(myAccount_[0], "oauth2:" + SCOPE, null, this,
        new OnTokenAcquired(), null);

It should prompt me to allow my app to access youtube.

Its working fine for my android application but not be able to ask on unity.

here is my code of android:

 public void YoutubeSubscriber() {
        AccountManager am = AccountManager.get(context);
        Account[] myAccount_ = AccountManager.get(context).getAccountsByType("com.google");
        Log.d("==============>all", String.valueOf(myAccount_));
        Log.d("==============>single", String.valueOf(myAccount_[0]));

        String SCOPE = "https://www.googleapis.com/auth/youtube";
        am.getAuthToken(myAccount_[0], "oauth2:" + SCOPE, null, null,
                new OnTokenAcquired(), null);
        Toast.makeText(this.context, myAccount_[0].toString(), Toast.LENGTH_SHORT).show();
    }

and using this in unity is like that.

            using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                info.text = "activity";
                activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
            }

            using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.thegamecontriver.androidlib.ToastExample"))
            {
                if (pluginClass != null)
                {

                    toastExample = pluginClass.CallStatic<AndroidJavaObject>("instance");
                    toastExample.Call("setContext", activityContext);

                    info.text = "After ";
                    toastExample.Call("YoutubeSubscriber");



                    /*
                    activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
                         toastExample.Call("YoutubeSubscriber");
                     }));
                     */
                }
            }

Note: i am able to see toast but permission is not prompting. please help

umair
  • 607
  • 4
  • 18

1 Answers1

0

You are likely missing the permission required (USE_CREDENTIALS) to use AccountManager.

From getAuthToken SDK reference:

NOTE: If targeting your app to work on API level 22 and before, USE_CREDENTIALS permission is needed for those platforms. See docs for this function in API level 22.

1.Go to <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk, Copy the AndroidManifest.xml file to your <ProjectName>Assets\Plugins\Android

2.Now open the copied Manifest file from <ProjectName>Assets\Plugins\Android and add <uses-permission android:name="android.permission.USE_CREDENTIALS"/> to it. Save, Build and Run. If this is a permission problem, that should now be solved.

What your AndroidManifest.xml should look like(Unity 5.4):

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.USE_CREDENTIALS"/>

    <application
    android:theme="@style/UnityThemeSelector"
    android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
        </activity>
    </application>
</manifest>
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I have added permissions. still no luck – umair Oct 03 '16 at 17:27
  • On the Java side, put the code in the `YoutubeSubscriber` function in a `try catch` clause and see if there is any new exception caught. Ouput the exception message with `Log.d`. – Programmer Oct 03 '16 at 17:47