Code Monkey home page Code Monkey logo

google-drive-rest-android's Introduction

Google-Drive-REST-Android

Google Drive REST API for android Cover This is a simple wrapper of around Google Drive REST API using OKHTTP.

Install via Gradle

Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:

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

Step 2. Add the dependency

dependencies {
        implementation 'com.github.tejpratap46:Google-Drive-REST-Android:VERSION'
}

Auth

Before you can do any request, you need to authenticate your app with google drive, Best way is to use Google Sign In For Android with Offline Access

Auth Steps

  1. Goto Google Console Credentials. Make sure Your Google Drive API is turned on for your Google Project.
  2. Create first Auth for Android App (For Google Sign In)
  3. Create another Auth for Web browser App (For Google Drive Rest API)

In Your Android App code, Auth using offline access, code:

String serverClientId = "CLIENT_ID_OF_WEB_BROWSER_API";
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
        .requestServerAuthCode(serverClientId)
        .requestEmail()
        .build();
        
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
startActivityForResult(mGoogleSignInClient.getSignInIntent(), REQUEST_CODE_GOOGLE_SIGN_IN);
  1. In your onActivityResult(Intent data)
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
    GoogleSignInAccount account = task.getResult(ApiException.class);
    String authCode = account.getServerAuthCode();
    
    ArrayList<GDAuthConfig.SCOPES> scopes = new ArrayList<>();
    scopes.add(GDAuthConfig.SCOPES.EMAIL);
    scopes.add(GDAuthConfig.SCOPES.DRIVE);
    scopes.add(GDAuthConfig.SCOPES.APP_FOLDER);
    
    final GDAuthConfig gdAuthConfig = new GDAuthConfig(REDIRECT_URI, CLIENT_ID, CLIENT_SECRET, scopes);
    
    // Use auth code to get AccessToken
    GDApiManager.getInstance().getAuthFromCodeAsync(authCode, gdAuthConfig, new GDAuthResponse.OnAuthResponseListener() {
        @Override
        public void onSuccess(final GDAuthResponse gdAuthResponse) {
            boolean isAuthDataSaved = GDAuthManager.getInstance().setAuthData(MainActivity.this, gdAuthResponse);
        }

        @Override
        public void onError(GDException exception) {

        }
    });
} catch (ApiException e) {
    Log.w(TAG, "Sign-in failed", e);
    updateUI(null);
}

Available API

  1. GDAuthResponse gdAuthResponse = getAuthFromCode(authCode, gdAuthConfig);
  2. GDAuthResponse gdAuthResponse = getAuthFromRefreshToken(context, authCode, previousAuthResponse);
  3. GDUserInfo gdUserInfo = getUserInfo(context, gdAuthResponse, gdAuthConfig);
  4. GDUploadFileResponse fileUploadResponse = uploadFile(context, gdAuthResponse, gdAuthConfig, fileToUpload, fileMime, uploadToAppFolder);
  5. File downloadedFile = downloadFile(context, gdAuthResponse, gdAuthConfig, gdResourceId, fileName)
  6. boolnea isFileDeleted = deleteFile(context, gdAuthResponse, gdAuthConfig, gdResourceId)

All API's are abailable as Async Requests.

Fo example, look into MainActivity of app module

google-drive-rest-android's People

Contributors

tejpratap46 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.