我必须在我的应用中添加使用Google功能登录。我已经尝试了几个教程,但是没有用。每当我从列表中选择一个帐户时,该弹出窗口都会关闭,并且什么也没有发生。我不知道这是什么问题,因为logcat上没有显示任何错误。
Login.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
public class Login extends AppCompatActivity {
GoogleSignInClient mGoogleSignInClient;
FirebaseAuth auth;
RelativeLayout googlesignin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
auth = FirebaseAuth.getInstance();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this,gso);
googlesignin = (RelativeLayout) findViewById(R.id.googlesignin);
googlesignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, 101);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = auth.getCurrentUser();
Intent i = new Intent(getApplicationContext(),Homewindow.class);
startActivity(i);
finish();
Toast.makeText(getApplicationContext(),"User Logged In Successfully",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(),"Login Failed",Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_login
<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:background="#00BFFF"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<RelativeLayout
android:id="@+id/loginmain"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/logincontents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/relativelayout_custom"
android:orientation="vertical"
android:padding="20dp">
<RelativeLayout
android:id="@+id/googlesignin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:orientation="horizontal"
android:background="@drawable/loginlayout">
<ImageView
android:id="@+id/googleicon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:src="@drawable/googleicon"/>
<TextView
android:id="@+id/googletext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/googleicon"
android:fontFamily="@font/averia_gruesa_libre"
android:text="Sign in with Google"
android:textColor="#db3236"
android:textSize="14dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/facebooksignin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_below="@id/googlesignin"
android:layout_marginTop="40dp"
android:background="@drawable/loginlayout">
<ImageView
android:id="@+id/facebookicon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:src="@drawable/facebookicon"/>
<TextView
android:id="@+id/facebooktext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:fontFamily="@font/averia_gruesa_libre"
android:layout_toRightOf="@id/facebookicon"
android:text="Sign in with Facebook"
android:textColor="#3b5998"
android:textSize="14dp"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="@+id/skipbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/facebooksignin"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:fontFamily="@font/averia_gruesa_libre"
android:paddingTop="5dp"
android:text="SKIP NOW >>"
android:textColor="#00BFFF"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/profileicon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_above="@id/logincontents">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:src="@drawable/profile_icon"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
在这里,我将onClickListener用于包含googleicon和文本的相对布局。
build.gradle(应用程序)
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationId "com.example.parethumukal"
minSdkVersion 18
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:18.1.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//noinspection GradleCompatible
implementation "com.google.android.material:material:1.0.0-rc01"
implementation 'com.github.bumptech.glide:glide:4.3.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
//noinspection GradleCompatible
def lifecycle_version = "2.0.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation files('libs\\YouTubeAndroidPlayerApi.jar')
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-database:18.0.0'
implementation 'com.google.firebase:firebase-storage:18.1.0'
implementation 'de.hdodenhof:circleimageview:3.0.0'
}
apply plugin: 'com.google.gms.google-services'
build.gradle(项目)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
logcat错误
07-23 21:48:22.994 21934-21934/com.example.parethumukal E/MultiWindowProxy: getServiceInstance failed!
07-23 21:48:27.924 21934-21982/com.example.parethumukal E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb37a4d60
我不知道我的代码是否有错误。代码是从Firebase网站获取的。
请帮助我。.
答案 0 :(得分:0)
您必须将server's client ID传递给requestIdToken
方法。
请修改您的GSO
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(/*Put your Request id Token from Firebase*/)
.requestEmail()
.build();
答案 1 :(得分:0)
尝试
GoogleSignInOptions gso;
GoogleApiClient googleApiClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.firebase_web_client_id))
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.enableAutoManage(this, null)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
答案 2 :(得分:-1)
添加此依赖项:
实施'com.google.firebase:firebase-auth:16.1.0'