Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
app
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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