Google+ 登錄

1.準備工作
首先得在谷歌開發(fā)控制臺https://console.developers.google.com 創(chuàng)建自己的項目,并配好相關(guān)的信息,如圖

Paste_Image.png

2.在你的project的gradle注入谷歌服務的依賴 classpath 'com.google.gms:google-services:3.0.0'

Paste_Image.png

然后在你的app的gradle中加入compile 'com.google.android.gms:play-services-auth:9.6.1'依賴, 并在最底部引入apply plugin: 'com.google.gms.google-services'

Paste_Image.png

記得在app目錄下的加入google-services.json文件

Paste_Image.png

好了,配置基本完成,接下來就是在你的登陸頁面擼登陸的代碼了
1.初始化Google 可以參考谷歌官方api https://developers.google.com/+/mobile/android/getting-started

/**
     * Google登錄初始化
     */
    public void initGooglePlus() {
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestId()
                .requestProfile()
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
    }

點擊登陸按鈕

/**
     * 登陸
     */
    public void googleSignIn() {
        Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(intent, G_SIGN_IN);

    }

回調(diào)

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == G_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }

獲取回調(diào)返回的用戶相關(guān)信息

 /**
     * Google plus 登陸回調(diào)
     */
    private void handleSignInResult(GoogleSignInResult result) {
        showLog("handleSignInResult----" + result.isSuccess());
        if (result.isSuccess()) {
            GoogleSignInAccount account = result.getSignInAccount();
            showLog("id--------" + account.getId() + "----name----" + account.getDisplayName() + "---photo--" + account.getPhotoUrl());
        }
    }

好了,簡單的完成google登陸了,不過還有退出等邏輯,這里就不寫了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容