项目作者: tejpratap46

项目描述 :
Google Drive REST API for android
高级语言: Java
项目地址: git://github.com/tejpratap46/Google-Drive-REST-Android.git
创建时间: 2019-09-13T21:16:15Z
项目社区:https://github.com/tejpratap46/Google-Drive-REST-Android

开源协议:MIT License

下载


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:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Step 2. Add the dependency

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

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:

  1. String serverClientId = "CLIENT_ID_OF_WEB_BROWSER_API";
  2. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  3. .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
  4. .requestServerAuthCode(serverClientId)
  5. .requestEmail()
  6. .build();
  7. GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
  8. startActivityForResult(mGoogleSignInClient.getSignInIntent(), REQUEST_CODE_GOOGLE_SIGN_IN);
  1. In your onActivityResult(Intent data)

    1. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
    2. try {
    3. GoogleSignInAccount account = task.getResult(ApiException.class);
    4. String authCode = account.getServerAuthCode();
    5. ArrayList<GDAuthConfig.SCOPES> scopes = new ArrayList<>();
    6. scopes.add(GDAuthConfig.SCOPES.EMAIL);
    7. scopes.add(GDAuthConfig.SCOPES.DRIVE);
    8. scopes.add(GDAuthConfig.SCOPES.APP_FOLDER);
    9. final GDAuthConfig gdAuthConfig = new GDAuthConfig(REDIRECT_URI, CLIENT_ID, CLIENT_SECRET, scopes);
    10. // Use auth code to get AccessToken
    11. GDApiManager.getInstance().getAuthFromCodeAsync(authCode, gdAuthConfig, new GDAuthResponse.OnAuthResponseListener() {
    12. @Override
    13. public void onSuccess(final GDAuthResponse gdAuthResponse) {
    14. boolean isAuthDataSaved = GDAuthManager.getInstance().setAuthData(MainActivity.this, gdAuthResponse);
    15. }
    16. @Override
    17. public void onError(GDException exception) {
    18. }
    19. });
    20. } catch (ApiException e) {
    21. Log.w(TAG, "Sign-in failed", e);
    22. updateUI(null);
    23. }

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