Reputation:
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.
}
Upvotes: 3
Views: 1406
Reputation: 15
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);
Upvotes: 0
Reputation: 15
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" />
Upvotes: 1
Reputation: 357
u have done everything perfect just change AccountKitActivity.ResponseType.CODE); to AccountKitActivity.ResponseType.TOKEN);
Upvotes: 13
Reputation: 15
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 ));
}
Upvotes: 0
Reputation: 198
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
Upvotes: 0