简体   繁体   中英

How to properly use Google Drive Rest API?

I have an application where I want to upload the SQLite database file to the user's google drive account.

As Google Android API will stop working I thought to implement REST API, implementing REST API requires authentication. I am using Google Sign-in, but REST API Requires some type of access token. How to get that?

I created an account in the developer console, enabled the Drive API, plus added the Scope for drive/auth.

I achieved the login functionality using the official documentation, https://developers.google.com/identity/sign-in/android/sign-in

Using this code I can get the email of the user as well as create a DriveService,

    GoogleSignInOptions googleSignInOptions = new 
    GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(new Scope(DriveScopes.DRIVE))
                .requestEmail()
                .build();

    googleSignInClient = GoogleSignIn.getClient(this, googleSignInOptions);

    Intent googleSignInIntent = googleSignInClient.getSignInIntent();
    startActivityForResult(googleSignInIntent, REQUEST_CODE_SIGN_IN);

In on activity result, I can get the user account, using the account I create a DriveServiceHelper

    mDriveServiceHelper = new DriveServiceHelper(getGoogleDriveService(getApplicationContext(), googleSignInAccount, "appName"));

Now the thing is when I try to create folder It shows me an error about (UserRecoverableAuthIOException),

See the code using which I create Folder

public Task<GoogleDriveFileHolder> createFolder(final String folderName, @Nullable final String folderId) {
        return Tasks.call(mExecutor, new Callable<GoogleDriveFileHolder>() {
            @Override
            public GoogleDriveFileHolder call() {
                GoogleDriveFileHolder googleDriveFileHolder = new GoogleDriveFileHolder();

                List<String> root;
                if (folderId == null) {
                    root = Collections.singletonList("root");
                } else {

                    root = Collections.singletonList(folderId);
                }
                File metadata = new File()
                        .setParents(root)
                        .setMimeType(DriveFolder.MIME_TYPE)
                        .setName(folderName);

                File googleFile = null;
                googleFile = mDriveService.files().create(metadata).execute();
                googleDriveFileHolder.setId(googleFile.getId());
                return googleDriveFileHolder;
            }
        });
    }

I tried solving the error, using various discussion threads available in StackOverflow none of them worked, can anyone guide me through this?

Given an android.accounts.Account , you can use AccountManager.blockingGetAuthToken , set this token in a GoogleCredential , and then pass that in as the HttpRequestInitializer to Drive.Builder .

String token = AccountManager.get(context)
    .blockingGetAuthToken(<android.accounts.Account>, "oauth2:" + DriveScopes.DRIVE, false);
GoogleCredential credential = new GoogleCredential().setAccessToken(token);
Drive service = new Drive.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
    .setApplicationName("app name")
    .build();

Eventually this token will expire, at which point you will need to call AccountManager.invalidateAuthToken and acquire a new one

If you have included the play-services-auth library then you can pass this credential instead:

GoogleAccountCredential credential = GoogleAccountCredential
    .usingOAuth2(context, Collections.singleton(DriveScopes.DRIVE))
    .setSelectedAccount(googleAccount.getAccount());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM