美文网首页
Android开发-Google登录

Android开发-Google登录

作者: Allens_Jiang | 来源:发表于2019-01-03 09:54 被阅读53次

    1 注册Google账户

    创建地址

    屏幕快照 2019-01-03 上午9.13.22.png

    2 申请开发者

    25美元呢

    开发者申请

    屏幕快照 2019-01-03 上午9.21.27.png

    3 创建凭据

    创建凭据

    选择客户端ID 屏幕快照 2019-01-03 上午9.27.06.png

    4 开发

    官网文档

    (1)环境配置

    在Android Studio中,选择工具> Android> SDK Manager
    滚动到包列表的底部,然后选择其他> Google Repository。该软件包将下载到您的计算机并安装在SDK环境中的android-sdk-folder / extras / google / google_play_services中。

    (2)添加Google Play服务

    在项目的顶级build.gradle文件中,确保包含Google的Maven存储库

    allprojects {
        repositories {
            google()
    
            // If you're using a version of Gradle lower than 4.1, you must instead use:
            // maven {
            //     url 'https://maven.google.com'
            // }
        }
    }
    

    然后,在您的应用级build.gradle文件中,将Google Play服务声明为依赖项:

    apply plugin: 'com.android.application'
        ...
    
        dependencies {
            compile 'com.google.android.gms:play-services-auth:15.0.1'
        }
    

    (3)配置Google登录和GoogleSignInClient对象

    • onCreate方法中
    // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    
    // Build a GoogleSignInClient with the options specified by gso.
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    
    • onStart方法中检查用户是否已使用Google登录您的应用
    // Check for existing Google Sign In account, if the user is already signed in
    // the GoogleSignInAccount will be non-null.
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    updateUI(account);
    

    (4)将Google登录按钮添加到您的应用 (可选)

    <com.google.android.gms.common.SignInButton
     android:id="@+id/sign_in_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    

    [图片上传失败...(image-c9a06c-1546480407359)]

    (5 )登录

    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    

    在活动的onActivityResult方法中为用户获取对象。

    @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);
        }
    }
    

    GoogleSignInAccount对象包含有关已登录用户的信息,例如用户的名称

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);
    
            // 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);
        }
    }
    

    (6)获取资料

    
    GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(getActivity());
    if (acct != null) {
      String personName = acct.getDisplayName();
      String personGivenName = acct.getGivenName();
      String personFamilyName = acct.getFamilyName();
      String personEmail = acct.getEmail();
      String personId = acct.getId();
      Uri personPhoto = acct.getPhotoUrl();
    }
    

    (7)退出登录

    private void signOut() {
        mGoogleSignInClient.signOut()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        // ...
                    }
                });
    }
    

    (8)断开帐户

    private void revokeAccess() {
        mGoogleSignInClient.revokeAccess()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        // ...
                    }
                });
    }
    

    (1)服务端需要token 但是 我们得到的token == null

       GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(getString(R.string.server_client_id))
                    .requestEmail()
                    .build();
    
    

    server_client_id哪里来的

    文档链接

    创建配置 选择web server 选择刚刚创建的

    相关文章

      网友评论

          本文标题:Android开发-Google登录

          本文链接:https://www.haomeiwen.com/subject/hfwxrqtx.html