Android开发-Google登录

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
选择刚刚创建的
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 6,370评论 1 9
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,596评论 2 45
  • 实习的目的是锻炼自己的动手能力,现在的我们都还是在父母的庇护下成长的,没有真正的面对生活和这个社会。 ...
    今晚暴富阅读 342评论 0 3
  • 网络,手机……信息眼花缭乱,我们每天都被这些绑架!我们没时间去思考,没时间去沉淀!所以需要我们摆脱网络的枷锁,有自...
    建业武亮亮阅读 240评论 0 1
  • 今天在女儿培训学校碰到一个家长,她说:'彤彤妈妈,你不是说自己瘦不下去吗,你怎么瘦了?'是呀,我是怎么瘦的呢?我们...
    风过__留痕阅读 1,257评论 0 1