3

throws an error when i try to get access token exact after phone verified

phoneLogin() this method load the UI and all the process goes perfect but when i try

`if (loginResult.getAccessToken() != null) {

                    Log.e("token", loginResult.getAccessToken().toString());
                    toastMessage = "Success:" + loginResult.getAccessToken().getAccountId();
                }

` this code it throws error is mention below

error:400: An internal consistency error has occurred: 406: No access token: cannot retrieve account

code

    public void phoneLogin() {
        final Intent intent = new Intent(LoginActivity.this, AccountKitActivity.class);
        AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                new AccountKitConfiguration.AccountKitConfigurationBuilder(
                        LoginType.PHONE,
                        AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN


        UIManager uiManager = new SkinManager(SkinManager.Skin.CLASSIC, Color.GRAY, R.drawable.bubble_background, SkinManager.Tint.BLACK, 5);
        configurationBuilder.setUIManager(uiManager);

        // ... perform additional configuration ...
        intent.putExtra(
                AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
                configurationBuilder.build());
        startActivityForResult(intent, APP_REQUEST_CODE);
    }
@Override
    protected void onActivityResult(
            final int requestCode,
            final int resultCode,
            final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == APP_REQUEST_CODE) { // confirm that this response matches your request
            AccountKitLoginResult loginResult = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
            String toastMessage;
            if (loginResult.getError() != null) {
                toastMessage = loginResult.getError().getErrorType().getMessage();
                //  showErrorActivity(loginResult.getError());
                Log.e("error", loginResult.getError().toString());
            } else if (loginResult.wasCancelled()) {
                toastMessage = "Login Cancelled";
            } else {
                if (loginResult.getAccessToken() != null) {
                    Log.e("token", loginResult.getAccessToken().toString());
                    toastMessage = "Success:" + loginResult.getAccessToken().getAccountId();
                } else {
                    toastMessage = String.format(
                            "Success:%s...",
                            loginResult.getAuthorizationCode().substring(0, 10));
                }

                // If you have an authorization code, retrieve it from
                // loginResult.getAuthorizationCode()
                // and pass it to your server and exchange it for an access token.

                // Success! Start your next activity...
                // goToMyLoggedInActivity();
                Log.d("success", toastMessage);
                Toast.makeText(this, "success", Toast.LENGTH_LONG).show();
                try {
                    AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                        @Override
                        public void onSuccess(Account account) {
                            String accountKitId = account.getId();

                            // Get phone number
                            PhoneNumber phoneNumber = account.getPhoneNumber();
                            String phoneNumberString = phoneNumber.toString();
                            SessionManager sessionManager = new SessionManager(getApplicationContext());
                            sessionManager.setPhone(phoneNumberString);
                            startActivity(new Intent(LoginActivity.this, MobileVerification.class));
                            finish();
                        }

                        @Override
                        public void onError(AccountKitError accountKitError) {

                        }
                    });
                } catch (Exception e) {
                    Log.d("catch", e.toString());
                }

            }

            // Surface the result to your user in an appropriate way.

        }

5 Answers5

13

u have done everything perfect just change AccountKitActivity.ResponseType.CODE); to AccountKitActivity.ResponseType.TOKEN);

altu
  • 357
  • 2
  • 13
1
i have to make demo for You and i successfully Logined with Facebbok accountKit(here not Applying With Permisson):




            <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"

            android:backgroundTint="#FFFFFF"
            android:background="#FFFFFF">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF"
                android:backgroundTint="#FFFFFF">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:gravity="center_vertical|center_horizontal"
                    android:layout_weight=".25"
                    android:background="#FFFFFF">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="Login with AccountKit"
                        android:id="@+id/textView6"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight=".25"
                    android:gravity="top|center_horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="This example shows you how to implement Facebook AccountKit in Android using Java."
                        android:id="@+id/textView7"
                        android:gravity="center_horizontal"
                        android:textColor="#000000" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight=".07"
                    android:gravity="center_horizontal">

                    <Button
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:text="Login with Email"
                        android:id="@+id/button"
                        android:onClick="emailLogin"
                        android:backgroundTint="#4E86FF"
                        android:textColor="#FFFFFF" />

                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight=".07"
                    android:gravity="center_horizontal"
                    android:layout_marginBottom="150dp">

                    <Button
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:text="Login with Phone"
                        android:id="@+id/button2"
                        android:onClick="smsLogin"
                        android:backgroundTint="#4E86FF"
                        android:textColor="#FFFFFF" />
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>

           InitialActivity.java:

            public class InitialActivity extends AppCompatActivity {

            public static int APP_REQUEST_CODE = 99;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                AccountKit.initialize(getApplicationContext());
                setContentView(R.layout.activity_initial);

                AccessToken accessToken = AccountKit.getCurrentAccessToken();
                Log.d("accesssToken:----",""+accessToken);

                if(accessToken != null){
                    goToMyLoggedInActivity();
                }
            }
            @Override
            protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == APP_REQUEST_CODE) { // confirm that this response matches your request
                    AccountKitLoginResult loginResult = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
                    String responseMessage;
                    if (loginResult.getError() != null) {
                        responseMessage = loginResult.getError().getErrorType().getMessage();
                        logAssert(loginResult.getError() + " - " + responseMessage);
                    } else if (loginResult.wasCancelled()) {
                        responseMessage = "Login Cancelled";
                        logAssert(responseMessage);
                    } else {
                        if (loginResult.getAccessToken() != null) {
                            Log.d("Token:-",""+loginResult.getAccessToken());
                            responseMessage = "Success: " + loginResult.getAccessToken().getAccountId();
                            logAssert(responseMessage);
                        } else {
                            responseMessage = String.format(
                                    "Success:%s...",
                                    loginResult.getAuthorizationCode().substring(0,10));
                            logAssert(responseMessage);
                        }

                        // If you have an authorization code, retrieve it from
                        // loginResult.getAuthorizationCode()
                        // and pass it to your server and exchange it for an access token.

                        // Success! Start your next activity...
                        goToMyLoggedInActivity();
                    }
                }
            }

            public void goToLogin(boolean isSMSLogin) {

                LoginType loginType = isSMSLogin ? LoginType.PHONE : LoginType.EMAIL;

                final Intent intent = new Intent(this, AccountKitActivity.class);
                AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                        new AccountKitConfiguration.AccountKitConfigurationBuilder(
                                loginType,
                                AccountKitActivity.ResponseType.TOKEN);

                UIManager uiManager = new SkinManager(
                        SkinManager.Skin.CONTEMPORARY,
                        getResources().getColor(R.color.colorBackground),
                        R.drawable.bg,
                        SkinManager.Tint.BLACK,
                        0.10);

                configurationBuilder.setUIManager(uiManager);

                intent.putExtra(
                        AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
                        configurationBuilder.build());
                this.startActivityForResult(intent, APP_REQUEST_CODE);
            }

            public void smsLogin(View v){
                goToLogin(true);
            }

            public void emailLogin(View v){
                goToLogin(false);
            }

            private void goToMyLoggedInActivity(){
                final Intent intent = new Intent(this, SecondActivity.class);
                this.startActivity(intent);
            }

            private void logAssert(String error) {
                Log.println(Log.ASSERT, "AccountKit", error);
            }
        }
    Second Activity:




        <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
    >

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_second" />

    </android.support.design.widget.CoordinatorLayout>

    SecondActivity.java:





        public class SecondActivity extends AppCompatActivity {

        TextView txtAccountKitID, txtUserLoginMode, txtUserLoginData;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);

            txtAccountKitID = (TextView) findViewById(R.id.txtAccountKitID);
            txtUserLoginMode = (TextView) findViewById(R.id.txtUserLoginMode);
            txtUserLoginData = (TextView) findViewById(R.id.txtUserLoginData);

            this.setUserInformation();

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
        }

        public void setUserInformation(){
            AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                @Override
                public void onSuccess(final Account account) {
                    // Get Account Kit ID
                    String accountKitId = account.getId();
                    logAssert("ID: " + accountKitId);

                    boolean SMSLoginMode = false;

                    // Get phone number
                    PhoneNumber phoneNumber = account.getPhoneNumber();
                    String phoneNumberString = "";
                    if (phoneNumber != null) {
                        phoneNumberString = phoneNumber.toString();
                        logAssert("Phone: " + phoneNumberString);
                        SMSLoginMode = true;
                    }

                    // Get email
                    String email = account.getEmail();
                    logAssert("Email: " + email);

                    txtAccountKitID.setText(accountKitId);
                    txtUserLoginMode.setText(SMSLoginMode ? "Phone:" : "Email:");
                    if (SMSLoginMode) {
                        txtUserLoginData.setText(phoneNumberString);
                    } else {
                        txtUserLoginData.setText(email);
                    }

                }

                @Override
                public void onError(final AccountKitError error) {
                    logAssert("Error: " + error.toString());
                }
            });
        }

        public void LogOut(View v){
            AccountKit.logOut();
            Intent initialActivity = new Intent(this, InitialActivity.class);
            this.startActivity(initialActivity);
            this.finish();
        }

        private void logAssert(String error) {
            Log.println(Log.ASSERT, "AccountKit", error);
        }

    }
    Android manifest:




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




        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <application
            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/AppTheme">
            <activity android:name=".InitialActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme"
                tools:replace="android:theme">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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


            <meta-data android:name="com.facebook.accountkit.ApplicationName" android:value="@string/app_name" />
            <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id" />
            <meta-data android:name="com.facebook.accountkit.ClientToken" android:value="@string/account_kit_client_token" />

            <activity
                android:name="com.facebook.accountkit.ui.AccountKitActivity"
                android:theme="@style/AppLoginTheme"
                tools:replace="android:theme">
            <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="@string/ak_login_protocol_scheme" />
            </intent-filter>
            </activity>


            <activity
                android:name=".SecondActivity"
                android:theme="@style/AppTheme"
                tools:replace="android:theme"></activity>



        </application>

    </manifest>

For Style:




        <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#4E86FF</item>
    <item name="colorPrimaryDark">#3e6bcc</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    </style>

    <style name="AppLoginTheme" parent="Theme.AccountKit" >
        <item name="android:windowNoTitle">true</item>
    </style>


    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Akash Shah
  • 15
  • 4
  • i m glad to hear that but i have solved it u can see accepted ans now i m facing problem here http://stackoverflow.com/questions/44071153/coordinatorlayout-not-scrooling-with-swiperefeshlayout –  May 19 '17 at 13:18
0
 public void goToLogin(boolean isSMSLogin) {

                LoginType loginType = isSMSLogin ? LoginType.PHONE : LoginType.EMAIL;

                final Intent intent = new Intent(this, AccountKitActivity.class);
                AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                        new AccountKitConfiguration.AccountKitConfigurationBuilder(
                                loginType,
                                AccountKitActivity.ResponseType.TOKEN);

                UIManager uiManager = new SkinManager(
                        SkinManager.Skin.CONTEMPORARY,
                        getResources().getColor(R.color.colorBackground),
                        R.drawable.bg,
                        SkinManager.Tint.BLACK,
                        0.10);

                configurationBuilder.setUIManager(uiManager);

                intent.putExtra(
                        AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
                        configurationBuilder.build());
                this.startActivityForResult(intent, APP_REQUEST_CODE);
            }

            public void smsLogin(View v){
                goToLogin(true);
            }

            public void emailLogin(View v){
                goToLogin(false);
            }

            private void goToMyLoggedInActivity(){
                final Intent intent = new Intent(this, SecondActivity.class);
                this.startActivity(intent);
            }

            private void logAssert(String error) {
                Log.println(Log.ASSERT, "AccountKit", error);
            }
        }

just change AccountKitActivity.ResponseType.CODE); to AccountKitActivity.ResponseType.TOKEN);

Akash Shah
  • 15
  • 4
0

You have to login first to get access token.

public void phoneLogin(final View view) {
  final Intent intent = new Intent(getActivity(), AccountKitActivity.class);
  AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
    new AccountKitConfiguration.AccountKitConfigurationBuilder(
      LoginType.PHONE,
      AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN
  // ... perform additional configuration ...
  intent.putExtra(
    AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
    configurationBuilder.build());
  startActivityForResult(intent, APP_REQUEST_CODE);
}

https://developers.facebook.com/docs/accountkit/android#smslogin

Qasim Hasnain
  • 198
  • 1
  • 1
  • 9
  • buddy i have verified once and .. second time i run app so i get null access token –  May 19 '17 at 07:11
  • From Facebook documentation. When initializing your intent extras, be sure to specify the AccountKitActivity.ResponseType that matches your application's authorization setting in the Facebook developer portal dashboard: TOKEN if the Enable Client Access Token Flow switch in your app's dashboard is ON, and CODE if it is OFF. – Qasim Hasnain May 19 '17 at 07:14
  • can u tell me why i m getting this error **400: An internal consistency error has occurred: 406: No access token: cannot retrieve account** –  May 19 '17 at 07:53
0

first time you don't have access token that means you redirect to Mobile Verfication Screen.Class and after getting access token You redirect to HomeActivity.class and Stored token in Shared Preference After killing the application when ever You Open Application then Redirect to the LoginActivity.class

 if (sessionManager.isLoggedIn() && accessToken != null) {
                startActivity(new Intent(SplashActivity.this,LoginActivity.class ));
            } else {
                startActivity(new Intent(SplashActivity.this,Mobile Verfication.class ));
            }
Akash Shah
  • 15
  • 4
  • LoginActivity.class Your issue is When You First Time Login With Facebook Account kit then Successfully Login But After killing Application and again app open at that time your application is not opened ? – Akash Shah May 19 '17 at 07:39
  • can u tell me why i m getting this error **400: An internal consistency error has occurred: 406: No access token: cannot retrieve account** –  May 19 '17 at 07:53
  • when you First Time Login with Facebook Account Kit Sdk, Account kit Giving Some token that token you Stored in Shared Preferenece – Akash Shah May 19 '17 at 07:57
  • may I know Actually What Issue is Occure ? – Akash Shah May 19 '17 at 07:59
  • forget all .... i just came to know that i dont get token even first time and error occurred 400: An internal consistency error has occurred: 406: No access token: cannot retrieve account code to retrive token is **if (loginResult.getAccessToken() != null) { Log.e("token", loginResult.getAccessToken().toString()); toastMessage = "Success:" + loginResult.getAccessToken().getAccountId(); }** –  May 19 '17 at 08:56
  • I have solved the issue check the accepted answer .but now i m facing issue here http://stackoverflow.com/questions/44071153/coordinatorlayout-not-scrooling-with-swiperefeshlayout –  May 19 '17 at 13:44