3

In my app two files one fragment and an activity.In onResume() of fragment I am calling activity for authorization.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    // Initialize credentials and service object.
    SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
    credential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(), Arrays.asList(SCOPES))
            .setBackOff(new ExponentialBackOff())
            .setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));

    mService = new com.google.api.services.drive.Drive.Builder(
            transport, jsonFactory, credential)
            .setApplicationName("AppName")
            .build();
}

In side this acvity I am saving emailId in SharedPreferences.This is working.

Then In side I am trying to access all folders of googledrive inside fragment file.

 private void listFolder(final String folderId, final String emal)
        throws IOException {
    mGOOSvc = new com.google.api.services.drive.Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),
            GoogleAccountCredential.usingOAuth2(this.getActivity(), Collections.singletonList(DriveScopes.DRIVE))
                    .setSelectedAccountName(emal)
    ).build();
    final ArrayList<String> al = new ArrayList<String>();
    final ArrayList<String> alIds = new ArrayList<String>();
    alIds.clear();
    request = mGOOSvc.children().list(folderId);
    new AsyncTask<String, String, String>() {
        @Override
        protected String doInBackground(String... params) {


            do {
                try {

                    ChildList children = request.execute();
                    for (ChildReference child : children.getItems()) {

                        String childId = child.getId();
                        File file = mGOOSvc.files().get(childId).execute();

                        if (!file.getExplicitlyTrashed() && file.getMimeType().equals("application/vnd.google-apps.folder")) {

                            al.add(file.getTitle());
                            alIds.add(file.getId());

                        }
                    }
                    request.setPageToken(children.getNextPageToken());
                } catch (Exception e) {
                    System.out.println("An error occurred: " + e.getMessage());
                    request.setPageToken(null);
                }
            } while (request.getPageToken() != null &&
                    request.getPageToken().length() > 0);

            return null;
        }


    }.execute();
}

It Shows exception on ChildList children = request.execute();: com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException..

(Interseting thing It works perfectly with one of my GoogleDrive Account. But in case of other accounts it shows this exception.I tried in in different way.I didn't use the my success full account Id as hard coded)

mikhail
  • 137
  • 2
  • 14
  • I checked Google Drive--Settings-- Manage Apps. Only one google drive account lists my apps name in"manage apps" . That account successfully work with my app. My assumption is that other account is not properly connected with my android app. May I know when the app will be added in this list. Is in the login time or authorizing page? – mikhail Sep 28 '15 at 20:34
  • The moment your app's user selects a new account, and this user goes through the authorization screen saying the user gives the app a permission to mess with her/his drive. see [this answer](http://stackoverflow.com/questions/32840511/accesing-google-drive-api-for-a-specific-account/32845250#32845250) – seanpj Sep 29 '15 at 13:52
  • @seanpj Thanks ... Now its works. But I have a question. The Authorization screen will come only one for an application in case of particular Accounr? Because I installed the app in a device and that time authorization screen showed for a specific account.Again I installed the app another device and login that the same account.On that time this authorization page never show(But works).. Any way is available to show the authorization page to each device in case of first login? – mikhail Sep 30 '15 at 07:31
  • 1
    I think (i'm not current in it), anytime there is a new app install + new account, the authorization is requested. So if you re-install the app with the same account it does not show. There is a pitfall in this, though, [see here](http://stackoverflow.com/questions/30172915/user-disconnecting-app-in-drive-causes-loss-of-data-under-file-scope) that can cause a major snafu if your users are too brave and test how things work (I think Daniel is an insider). – seanpj Sep 30 '15 at 12:04
  • Thanyou seanpj for the information.Can we revokes authorization via programatically? (Similar to - Settings > Manage Apps > Disconnect From Drive) – mikhail Sep 30 '15 at 14:25
  • Don't know, definitely NOT through the GDAA, REST Apis. Maybe some authorization related API? – seanpj Sep 30 '15 at 15:01

0 Answers0