Xamarin.Android 的 Google 登入

Google  的 Sing-In 功能,Xamarin 放在 Google Play Services - Plus 內。
所以我們要先引入這個 Component 

然後一樣是在 https://developers.google.com/mobile/add 平台上,選擇 Android 後,加入 Sign-In 功能。

加入憑證的 SHA-1 指紋。這樣就完成設定的部分。


然後程式碼部分,請大家注意這個 GoogleApiClientBuilder 類別
Google 已經將許多呼叫 Google 服務的方法以這個類別作為統一入口。

mGoogleApiClient = new GoogleApiClient.Builder (this)
                .AddConnectionCallbacks (this)
                .AddOnConnectionFailedListener (this)
                .AddApi (PlusClass.API)
                .AddScope (new Scope (Scopes.Profile))
                .Build ();

以這個類別建構起的實體運行 Connect() 後,即可串接Google Service

mGoogleApiClient.Connect ();

而後以下列方法獲得資料

var person = PlusClass.PeopleApi.GetCurrentPerson (mGoogleApiClient);


再運用這段程式碼時,請注意在UI這邊要放 Google 自製的 SignInButton 物件
再由程式這邊實作 

GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener

 

<com.google.android.gms.common.SignInButton
            android:id="@+id/sign_in_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="visible"
            tools:visibility="gone" />

 

程式碼:https://github.com/FangHuaiAn/Xamarin-AndroidTips

參考:https://developers.google.com/identity/sign-in/android/sign-in?configured=true

posted @ 2016-04-20 14:15  胖胖安  阅读(419)  评论(0)    收藏  举报