Skip to content

Get auto refreshing access-token easily using this library. This library automate the flow to get access token from the server.

License

Notifications You must be signed in to change notification settings

ErrorxCode/EasyAuth

Repository files navigation

Google API-Authentication library

Build Status Library version Android Arsenal

Fetching Google OAuth tokens was never easy

An android library through which you can easily fetch access tokens for Google APIs. Now assign the work of authentication to this library and concentrate on your main work.

image

Features

  • Easily get access and refresh tokens.
  • Auto refreshing access token
  • Auto validation, expiration, and fetching of tokens
  • Smooth google sign-in flow with callbacks

Implementation

Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

in your module (app) build.gradle:

	dependencies {
	      implementation 'com.github.ErrorxCode:EasyInsta:2.5.0'
	}

Acknowledgement

Usage

First of all, initialize the library passing your cloud credentials.

EasyAuth easyAuth = new EasyAuth(CLIENT_ID,CLIENT_SECRET);

Note: Both of these must be of Web application credentials. Note: All the methods run synchronously, put this in the background thread if needed.

Automatic way

To automatically get access token, use autoRefreshingAccessToken() method.

String accessToken = easyAuth.autoRefreshingAccessToken(this, API_SCOPE);

You also need to overwride onActivityResult and call easyAuth.onActivityResult() method in that.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        easyAuth.onActivityResult(resultCode, data, "https://your.redirect.url", new EasyAuth.LoginFlowCallback() {
            @Override
            public void onSuccess(@NonNull String accessToken, @NonNull String refreshToken, @NonNull String authorizationCode) {
                // Get your access token from here.
            }

            @Override
            public void onFailed(@NonNull Exception e) {
                // Check the exception
            }
        });
    }

For the first time, it will return null and starts the sign-in flow. You will receive your access token as a result of that sign-in flow.

Manual way

If you already have an authorization code or refresh token, you can also manually fetch an access tokens from them.

try {
    String refreshToken = easyAuth.getRefreshCode("AUTHORIZATION_CODE");  // Save this for future.
    String accessToken = easyAuth.getAccessToken(refreshToken);
} catch (Exception e) {
    e.printStackTrace();
}

Note: AUTHORIZATION_CODE can only be used once. So in this way, you have to save refreshToken on the device to get access_token directly.

If you don't have an authorization code, you can get one by authentication user with a google account.

easyAuth.startLoginFlow(this,"API_SCOPE");

You also need to overwride onActivityResult and call easyAuth.onActivityResult() method in that.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        easyAuth.onActivityResult(resultCode, data, "https://your.redirect.url", new EasyAuth.LoginFlowCallback() {
            @Override
            public void onSuccess(@NonNull String accessToken, @NonNull String refreshToken, @NonNull String authorizationCode) {
                // Get your access token from here.
            }

            @Override
            public void onFailed(@NonNull Exception e) {
                // Check the exception
            }
        });
    }

That's it

Yhe, Now it's easy to manage auth tokens. It's time to contribute now. You can add different scopes constant in this class.

License

Copyright 2015 Turhan OZ

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Get auto refreshing access-token easily using this library. This library automate the flow to get access token from the server.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages