目錄
1.配置Google Cloud
2.添加 Google Play 服務(wù)
3.添加Google sign代碼
4.常見的CommonStatusCodes
后補Web clien(Auto-created for Google Sign-in)由來
1.配置Google Cloud
? ? ? ? 首先要在 Google Cloud?中創(chuàng)建一個項目。左側(cè)菜單->API和服務(wù)->憑據(jù)。進入之后先配置同意屏幕。
?
填寫必必要的信息,應(yīng)用名稱、用戶支持電子郵件地址、開發(fā)者電子郵件地址?。
到第3步添加測試用戶(只有填寫的測試用戶才能登錄測試)
?OAuth 同意屏幕 配置完成之后選擇憑據(jù) CREATECREDENTIALS?
?選擇Android平臺應(yīng)用,填寫基礎(chǔ)的信息
創(chuàng)建之后信息如下:
關(guān)鍵點在一些其他的文檔教程中會發(fā)現(xiàn)存在一條 Web client(Auto-created for Google Sign-in),如下圖所示:這里暫時先不講,后續(xù)會講到這點
2.添加 Google Play 服務(wù)
在項目的頂級?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'
// }
}
}
然后,在應(yīng)用級?build.gradle
?文件中,將?Google Play 服務(wù)聲明為依賴項:
apply plugin: 'com.android.application'
...
dependencies {
implementation 'com.google.android.gms:play-services-auth:20.2.0'
}
3.添加Google sign代碼
? ? ? ? 廢話不多,直接上代碼
public class LoginActivity extends AppCompatActivity {
private ActivityLoginBinding binding;
// Google
private GoogleSignInClient mGoogleSignInClient;
private static final int RC_SIGN_IN = 9001;
private static final String serverClientId = "49031722657-r22t4obi9v020qpba9d9f0eonchlcqn4.apps.googleusercontent.com";
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityLoginBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
final Button loginButton = binding.login;
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
final Button logoutButton = binding.logout;
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signOut();
}
});
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestId()
.requestEmail()
.requestIdToken(serverClientId)
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
mGoogleSignInClient.signOut().addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getApplicationContext(), "signOut Complete!", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach a listener.
Task<GoogleSignInAccount> completedTask = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully
String result = "id = " + account.getId() + "\n" + "token = " + account.getIdToken();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
String result = "signInResult:failed code=" + e.getStatusCode();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
}
}
????????創(chuàng)建GoogleSignInOptions時可以選擇requestId()、requestEmail()、requestProfile()、requestIdToken(serverClientId)。
? ? ? ? getEmail() 獲取用戶的電子郵件地址
? ? ? ? getId()?獲取用戶的 Google ID(供客戶端使用)
????????getIdToken()?獲取用戶的 ID 令牌,如果要獲取idToken,則創(chuàng)建時需要用到requestIdToken(serverClientId),其中serverClientId就是創(chuàng)建的憑據(jù)中的客戶端ID值。
? ? ? ? 注意:如果使用在第1步中創(chuàng)建的signTest的客戶端ID的話在登錄時會返回錯誤碼 10。提示應(yīng)用配置錯誤。還記得在上述文中提到的Web client(Auto-created for Google Sign-in)。在官方的文檔中有這樣一段描述
? ? ? ? 按照官方描述我怎么也找不到自動創(chuàng)建的Web client。找不到就自己創(chuàng)建一個咯。在憑據(jù)中創(chuàng)建一個類型為Web應(yīng)用的客戶端ID即可,創(chuàng)建完成之后使用這個客戶端ID就可以正常登錄了。美滋滋。。。
4.常見的CommonStatusCodes
值 | 定義 | 描述 |
0 | SUCCESS | 成功。 |
5 | INVALID_ACCOUNT |
客戶端試圖使用指定的無效帳戶名連接到服務(wù)。 |
7 | NETWORK_ERROR |
網(wǎng)絡(luò)錯誤。(國內(nèi)一般是未科學(xué)上網(wǎng)導(dǎo)致) |
10 | DEVELOPER_ERROR |
應(yīng)用程序配置錯誤。此錯誤不可恢復(fù),將被視為致命錯誤。開發(fā)人員應(yīng)該在這之后查看日志,以確定更多可操作的信息。 |
16 | CANCELED |
客戶端斷開連接或主動取消(PendingResult.cancel())。 |
22 | RECONNECTION_TIMED_OUT | 連接超時。 |
????????詳細地址:https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes#DEVELOPER_ERROR
搬運地址:https://developers.google.com/identity/sign-in/android/start
GitHub地址:https://github.com/googlesamples/google-services.git
后補Web clien(Auto-created for Google Sign-in)由來
????????在官方的接入文檔中有一個Configure a project的按鈕。使用這里的按鈕來創(chuàng)建客戶端ID的流程如下:?
?這里選擇Android,輸入包名,指紋,按照這種方式創(chuàng)建出來的客戶端ID會有2個。
?一個是Android,一個是Web應(yīng)用。終于知道Web client(Auto-created for Google Sign-in)是怎么來的文章來源:http://www.zghlxwxcb.cn/news/detail-429374.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-429374.html
到了這里,關(guān)于安卓接入Google登錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!