我正在尝试通过移动应用程序通过后端服务器进行身份验证。
我正在关注这个文档。https://developers.google.com/identity/sign-in/android/sign-in https://developers.google.com/identity/sign-in/android/sign-in然而,也存在一些错误。RC_SIGN_IN
and updateUI()
无法解决。
我的代码是这样的
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
mSignInButton = findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_LONG).show();
Intent signIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signIntent, RC_SIGN_IN);
}
});
@Override
protected void onStart() {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
super.onStart();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
String idToken = account.getIdToken();
// Send Id Token to the backend and validate here
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
Update
现在按钮本身不起作用。
xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Include the main content -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
<!-- Navigation bar -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
我怎样才能解决这个问题?
您只需更换即可RC_SIGN_IN
具有 int 值。它可以是任何值,但不能使用 1 作为其值。执行如下操作:
startActivityForResult(signIntent, 1);
并将活动结果中的 if 代码更改如下:
if (requestCode == 1)
还将登录按钮单击代码更改为此(删除开关盒):
mSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
}
});
这是因为您正在调用按钮的 click 方法,然后再次检查是否单击了同一个按钮,这就是我认为它不起作用的原因。
现在对于updateUI
方法,这个方法应该由你定义。基本上,这是让您的应用程序更改用户登录应用程序时显示的内容。如果您想在以下时间打开新活动signedIn()
您可以使用Intent
通过改变updateUI(account)
在活动结果和 onstart 事件中达到意图:
startActivity(new Intent(MainActivity.this, SecondActivity.class));
并获取登录的是哪个帐户SecondActivity
:
GoogleSignInAccount account = GoogleSignIn.g etLastSignedInAccount(this); //use this in onCreate
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)