billtom20 / GAppAuth

SDK for Google SignIn (Based on AppAuth)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GAppAuth

latest version: 1.6

GAppAuth is an SDK for applications installed on Android devices like phones, tablets use Google's OAuth 2.0 endpoints to authorize access to Google APIs.

Usage

  1. An OAuth2 client ID for Google Sign In must be created. The quick-start configurator can be used to generate this, or it can be done directly on the Google Developer Console. Or refer to this document

  2. Add codes below in project build.gradle file.

allprojects {
	repositories {
		...
		maven { url 'https://www.jitpack.io' }
	}
}
  1. Add codes below in app build.gradle file.
dependencies {
    implementation 'com.github.billtom20:GAppAuth:<latest_version>'
    ...
}
  1. Add your Google client ID PREFIX in app build.gradle file. Google client ID should look something like PREFIX.apps.googleusercontent.com, where PREFIX is an alphanumeric string unique to your client ID.
manifestPlaceholders = [
        clientId_prefix: "PREFIX"
]
  1. Replace your own signingConfigs in build.gradle(Module:GAppAuth.app), it should be the keystore file used to generate the SHA-1 value configured on Google Developer Console.

API description

  1. SignIn
GSignInOptions signInOptions = new GSignInOptions.Builder(GSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                // .requestScopes("email")
                .build();
GAppAuth appAuth = new GAppAuth(this, signInOptions);

findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        appAuth.signIn(RC_AUTH);
    }
});
  1. Process SignIn results
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_AUTH) {
        appAuth.parseAuthResultFromIntent(data, new GAppAuth.OnSignInListener() {
            @Override
            public void onSuccess(@NonNull GSignInAccount account) {
                Log.d(TAG, account.toString();
            }

            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "onFailure", e);
            }
        });
    }
}
  1. SignOut
appAuth.signOut(new GAppAuth.OnSignOutListener() {
    @Override
    public void onSuccess() {
        log.append("signOut success\r\n");
    }

    @Override
    public void onFailure(@NonNull Exception e) {
        Log.e(TAG, "onFailure", e);
    }
});

About

SDK for Google SignIn (Based on AppAuth)

License:Apache License 2.0


Languages

Language:Java 100.0%