1 注冊(cè)Google賬戶
2 申請(qǐng)開發(fā)者
25美元呢
3 創(chuàng)建憑據(jù)
4 開發(fā)
(1)環(huán)境配置
在Android Studio中,選擇工具> Android> SDK Manager
骄瓣。
滾動(dòng)到包列表的底部,然后選擇其他> Google Repository
算芯。該軟件包將下載到您的計(jì)算機(jī)并安裝在SDK環(huán)境中的android-sdk-folder / extras / google / google_play_services
中努咐。
(2)添加Google Play服務(wù)
在項(xiàng)目的頂級(jí)build.gradle文件中嫂侍,確保包含Google的Maven存儲(chǔ)庫(kù)
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'
// }
}
}
然后恃疯,在您的應(yīng)用級(jí)build.gradle文件中漏设,將Google Play服務(wù)聲明為依賴項(xiàng):
apply plugin: 'com.android.application'
...
dependencies {
compile 'com.google.android.gms:play-services-auth:15.0.1'
}
(3)配置Google登錄和GoogleSignInClient對(duì)象
- 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登錄您的應(yīng)用
// 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登錄按鈕添加到您的應(yīng)用 (可選)
<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);
}
在活動(dòng)的onActivityResult方法中為用戶獲取對(duì)象。
@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對(duì)象包含有關(guān)已登錄用戶的信息今妄,例如用戶的名稱
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)服務(wù)端需要token 但是 我們得到的token == null
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.server_client_id))
.requestEmail()
.build();
server_client_id
哪里來(lái)的