我正在尝试在我的Android应用中实现firebase-ui-auth:1.0.0。我首先从github(https://github.com/firebase/FirebaseUI-Android)下载并安装FirebaseUI-Android演示。我使用此演示应用程序使用所有四种方法(电子邮件,Google,Facebook和Twitter)成功创建了Firebase用户。
现在我正在尝试在我的Android应用程序中实现firebase-ui-auth。电子邮件和Google方法运行正常; Facebook和Twitter方法失败了。单击“使用Facebook登录”时,我会短暂地看到进度条,然后返回到登录页面,但没有解释登录失败的原因。单击“使用Twitter登录”时,会出现Twitter页面“授权myApp使用您的帐户?”;单击“允许”后,我将返回登录页面,并显示“Toast”,表示“无法完成操作”
我已经在Facebook和Twitter上设置了我的应用程序。将他们的应用程序ID和应用程序机密插入我的应用程序的Firebase控制台,并在Facebook和Twitter控制台中插入Firebase回调URL。我还在Facebook控制台中插入了我的Android Key Hash。
非常感谢您提前了解如何让Facebook和Twitter认证工作。
我的应用的build.gradle如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.lbconsulting.coachslog"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
testCompile 'junit:junit:4.12'
// Google
compile 'com.google.android.gms:play-services-auth:9.8.0'
// Firebase
compile 'com.google.firebase:firebase-database:9.8.0'
compile 'com.google.firebase:firebase-auth:9.8.0'
compile 'com.google.firebase:firebase-config:9.8.0'
compile 'com.google.android.gms:play-services-appinvite:9.8.0'
compile 'com.google.firebase:firebase-messaging:9.8.0'
compile 'com.google.android.gms:play-services-ads:9.8.0'
compile 'com.google.firebase:firebase-crash:9.8.0'
compile 'com.google.firebase:firebase-invites:9.8.0'
// Firebase UI
// compile 'com.firebaseui:firebase-ui-database:0.4.0'
// FirebaseUI Database only
compile 'com.firebaseui:firebase-ui-database:1.0.0'
// FirebaseUI Auth only
compile 'com.firebaseui:firebase-ui-auth:1.0.0'
// FirebaseUI Storage only
// compile 'com.firebaseui:firebase-ui-storage:1.0.0'
// Timber and Butterknife
compile 'com.jakewharton.timber:timber:4.1.0'
compile 'com.jakewharton:butterknife:8.0.1'
// Event Bus
compile 'org.greenrobot:eventbus:3.0.0'
// Gson
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
// Facebook
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'org.jetbrains:annotations-java5:15.0'
}
apply plugin: 'com.google.gms.google-services'
在我的应用程序onCreate()中,我使用以下代码初始化Firebase Auth:
// Initialize Firebase Auth
mFirebaseAuth = FirebaseAuth.getInstance();
if (mFirebaseAuth.getCurrentUser() == null) {
startActivity(AuthUiActivity.createIntent(AthletesActivity.this));
finish();
return;
} else {
我的AuthUiActivity代码是:
public class AuthUiActivity extends AppCompatActivity {
private static final String FIREBASE_TOS_URL =
"https://www.firebase.com/terms/terms-of-service.html";
private static final int RC_SIGN_IN = 100;
@BindView(android.R.id.content)
View mRootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Timber.i("onCreate()");
FirebaseAuth auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(AthletesActivity.createIntent(this));
finish();
}
ButterKnife.bind(this);
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder()
.setTheme(AuthUI.getDefaultTheme())
.setLogo(AuthUI.NO_LOGO)
.setProviders(getSelectedProviders())
.setTosUrl(getSelectedTosUrl())
.setIsSmartLockEnabled(true)
.build(),
RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Timber.i("onActivityResult()");
if (requestCode == RC_SIGN_IN) {
handleSignInResponse(resultCode, data);
return;
}
showSnackbar(R.string.unknown_response);
}
@MainThread
private void handleSignInResponse(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
startActivity(AthletesActivity.createIntent(this));
finish();
return;
}
if (resultCode == RESULT_CANCELED) {
showSnackbar(R.string.sign_in_cancelled);
return;
}
if (resultCode == ResultCodes.RESULT_NO_NETWORK) {
showSnackbar(R.string.no_internet_connection);
return;
}
showSnackbar(R.string.unknown_sign_in_response);
}
@MainThread
private List<IdpConfig> getSelectedProviders() {
List<IdpConfig> selectedProviders = new ArrayList<>();
selectedProviders.add(new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build());
selectedProviders.add(
new IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER)
.setPermissions(getFacebookPermissions())
.build());
selectedProviders.add(
new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER)
.setPermissions(getGooglePermissions())
.build());
selectedProviders.add(new IdpConfig.Builder(AuthUI.TWITTER_PROVIDER).build());
return selectedProviders;
}
@MainThread
private List<String> getFacebookPermissions() {
List<String> result = new ArrayList<>();
// TODO: do we need to get Facebook user_friends and user_photos
// result.add("user_friends");
// result.add("user_photos");
return result;
}
@MainThread
private List<String> getGooglePermissions() {
List<String> result = new ArrayList<>();
// TODO: do we need to get Google Games and Drive_file?
// result.add(Scopes.GAMES);
// result.add(Scopes.DRIVE_FILE);
return result;
}
@MainThread
private String getSelectedTosUrl() {
// if (mUseGoogleTos.isChecked()) {
// return GOOGLE_TOS_URL;
// }
// TODO: create terms of service and save it in Firebase
return FIREBASE_TOS_URL;
}
@MainThread
private void showSnackbar(@StringRes int errorMessageRes) {
if (mRootView != null) {
Snackbar.make(mRootView, errorMessageRes, Snackbar.LENGTH_LONG).show();
}
}
public static Intent createIntent(Context context) {
Intent intent = new Intent();
intent.setClass(context, AuthUiActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
}
答案 0 :(得分:0)
将以下存储库添加到您的gradle:
应用程序/的build.gradle
android {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
...