我正在尝试从移动应用程序向后端服务器进行身份验证。我正在关注这个文档。https://developers.google.com/identity/sign-in/android/sign-in
但是,有一些错误。RC_SIGN_IN并且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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在按钮本身不起作用。
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>
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
除了替换RC_SIGN_IN为 int 值外,您什么都不用做。它可以是任何东西,但使用 1 作为它的值。执行以下操作:
startActivityForResult(signIntent, 1);
Run Code Online (Sandbox Code Playgroud)
并更改活动结果中的 if 代码如下:
if (requestCode == 1)
Run Code Online (Sandbox Code Playgroud)
还将登录按钮单击代码更改为此(删除切换案例):
mSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是因为您正在对按钮调用 click 方法,然后再次检查是否单击了相同的按钮,这就是我认为它不起作用的原因。
现在对于updateUI方法,该方法应该由您定义。基本上,这是让您的应用在用户登录应用时更改向用户显示的内容。如果您想在signedIn()可以使用时打开新活动,请Intent通过将updateUI(account)活动结果中的和 onstart 事件更改为意图:
startActivity(new Intent(MainActivity.this, SecondActivity.class));
Run Code Online (Sandbox Code Playgroud)
并获取在以下位置登录的帐户SecondActivity:
GoogleSignInAccount account = GoogleSignIn.g etLastSignedInAccount(this); //use this in onCreate
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6416 次 |
| 最近记录: |