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)