Md. Shofiulla
Md. Shofiulla

Reputation: 2315

Integrating Snap-kit in android

I am trying to add snap-kit in my application and I have integrated everything as their documentation. App successfully launched Snapchat app but it's not login it shows an error Something is wrong Here is my complete code and other implementation. This is the official documentation that I have followed https://kit.snapchat.com/docs/login-kit-android

MainActivity

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private static final String TAG = "MAIN_ACTIVITY";
    String query = "{me{bitmoji{avatar},displayName}}";
    String variables = null;
    boolean isUserLoggedIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        isUserLoggedIn = SnapLogin.isUserLoggedIn(this);
        if (isUserLoggedIn)
            binding.btnLogin.setText("Logout");
        else
            binding.btnLogin.setText("Logout");
        binding.btnLogin.setOnClickListener(v -> {
            SnapLogin.getAuthTokenManager(this).startTokenGrant();
            binding.progress.setVisibility(View.VISIBLE);
        });
        SnapLogin.getLoginStateController(this).addOnLoginStateChangedListener(mLoginStateChangedListener);
        

        SnapLogin.fetchUserData(this, query, null, new FetchUserDataCallback() {
            @Override
            public void onSuccess(@Nullable UserDataResponse userDataResponse) {
                if (userDataResponse == null || userDataResponse.getData() == null) {
                    return;
                }

                MeData meData = userDataResponse.getData().getMe();
                if (meData == null) {
                    return;
                }

                binding.txtName.setText(userDataResponse.getData().getMe().getDisplayName());

                if (meData.getBitmojiData() != null) {
                    Glide.with(MainActivity.this)
                            .load(meData.getBitmojiData().getAvatar())
                            .into(binding.imgProfile);
                }
            }

            @Override
            public void onFailure(boolean isNetworkError, int statusCode) {

            }
        });
    }

    final LoginStateController.OnLoginStateChangedListener mLoginStateChangedListener =
            new LoginStateController.OnLoginStateChangedListener() {
                @Override
                public void onLoginSucceeded() {
                    // Here you could update UI to show login success
                    binding.progress.setVisibility(View.VISIBLE);
                    Log.d(TAG, "onLoginSucceeded: ");
                    binding.btnLogin.setText("Logout");
                }

                @Override
                public void onLoginFailed() {
                    // Here you could update UI to show login failure
                    binding.progress.setVisibility(View.VISIBLE);
                    Log.d(TAG, "onLoginFailed: ");
                }

                @Override
                public void onLogout() {
                    // Here you could update UI to reflect logged out state
                    binding.progress.setVisibility(View.VISIBLE);
                    Log.d(TAG, "onLogout: ");
                }
            };
}

Manifests

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iotait.snapchatandroid">

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

    <queries>
        <package android:name="com.snapchat.android" />
    </queries>

    <application
        android:name=".application.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SnapchatAndroidDemo">

        <meta-data
            android:name="com.snapchat.kit.sdk.clientId"
            android:value="**********************" />
        <meta-data
            android:name="com.snapchat.kit.sdk.redirectUrl"
            android:value="*********************" />
        <meta-data
            android:name="com.snapchat.kit.sdk.scopes"
            android:resource="@array/snap_connect_scopes" />

        <activity android:name=".ui.sign.SigninActivity"
            android:screenOrientation="nosensor"/>
        <activity android:name=".ui.intro.IntroActivity"
            android:screenOrientation="nosensor"/>
        <activity android:name=".ui.login.LoginActivity"
            android:screenOrientation="nosensor"/>
        <activity
            android:name=".ui.splash.SplashActivity"
            android:screenOrientation="nosensor"
            android:theme="@style/Theme.splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ui.MainActivity"
            android:screenOrientation="nosensor" />
        <activity
            android:name="com.snapchat.kit.sdk.SnapKitActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="lmk"
                    android:path="/oauth2"
                    android:scheme="lmk" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

Dependencies

implementation([
            'com.snapchat.kit.sdk:login:1.1.4',
            'com.snapchat.kit.sdk:core:1.1.4'
    ])

Upvotes: 3

Views: 920

Answers (1)

Usama Altaf
Usama Altaf

Reputation: 118

You need to add these line in the application tag

<meta-data android:name="com.snapchat.kit.sdk.clientId" android:value="1aab4ace-3f06-487d-bc85************"/>
    <meta-data android:name="com.snapchat.kit.sdk.redirectUrl" android:value="https:********"/>
    <meta-data android:name="com.snapchat.kit.sdk.scopes" android:resource="@array/snap_connect_scopes"/>

Use like this

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iotait.snapchatandroid">

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

    <queries>
        <package android:name="com.snapchat.android" />
    </queries>

    <application
        android:name=".application.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SnapchatAndroidDemo">
<meta-data android:name="com.snapchat.kit.sdk.clientId" android:value="1aab4ace-3f06-487d-bc85************"/>
    <meta-data android:name="com.snapchat.kit.sdk.redirectUrl" android:value="https:********"/>
    <meta-data android:name="com.snapchat.kit.sdk.scopes" android:resource="@array/snap_connect_scopes"/>
        <activity android:name=".intro.IntroActivity"/>
        <activity android:name=".login.LoginActivity"/>
        <activity
            android:name=".splash.SplashActivity"
            android:theme="@style/Theme.splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" />

        <activity
            android:name="com.snapchat.kit.sdk.SnapKitActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="lmk" android:host="lmk" android:path="/oauth2"/>
            </intent-filter>

        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />


    </application>

</manifest>

Upvotes: 1

Related Questions