Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronous auth process problem. #73

Open
isabsent opened this issue Feb 22, 2016 · 6 comments
Open

Synchronous auth process problem. #73

isabsent opened this issue Feb 22, 2016 · 6 comments
Assignees
Labels

Comments

@isabsent
Copy link

As you mentioned in this SO answer synchronous auth process is possible. I need to implement it in one single OneDriveActivity (avoiding Fragments and Application) as:

public class OneDriveActivity extends Activity {
    public static String
        ACCESS_KEY_NAME = "ACCESS_KEY_ONEDRIVE",
        ACCESS_SECRET_NAME = "ACCESS_SECRET_ONEDRIVE";
    private static String
        CLIENT_ID = "...is hidden...",
        ADAL_CLIENT_ID = "...is hidden...",
        ADAL_REDIRECT_URL = "http://...is hidden...",
        OAUTH_2 = "oauth2:";
    private static String[] SCOPES_ARRAY = new String[] {"onedrive.readwrite", "onedrive.appfolder", "wl.offline_access"};

    private Button mSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_onedrive);

        mSubmit = (Button) findViewById(R.id.auth_button);
        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSubmit.setEnabled(false);
                try {
                    OneDriveClient.Builder builder = new OneDriveClient.Builder().fromConfig(createConfig());
                    IOneDriveClient service = builder.loginAndBuildClient(OneDriveActivity.this);
                    if (service != null) {    //is never called
                        Clouds.getOneDriveCloud().setService(service);
                        setResult(RESULT_OK);
                        finish();
                    }
                } catch (Exception e){
                    e.printStackTrace();    //is never called
                } finally {
                    mSubmit.setEnabled(true);    //is never called
                }
            }
        });
    }

    private IClientConfig createConfig() {
        final MSAAuthenticator msaAuthenticator = new MSAAuthenticator() {
            @Override
            public String getClientId() {
                return CLIENT_ID;
            }

            @Override
            public String[] getScopes() {
                return SCOPES_ARRAY;
            }
        };

        final ADALAuthenticator adalAuthenticator = new ADALAuthenticator() {
            @Override
            public String getClientId() {
                return ADAL_CLIENT_ID;
            }

            @Override
            protected String getRedirectUrl() {
                return ADAL_REDIRECT_URL;
            }
        };

        return DefaultClientConfig.createWithAuthenticators(msaAuthenticator, adalAuthenticator);
    }
}

Last code string that is called is:

IOneDriveClient service = builder.loginAndBuildClient(OneDriveActivity.this);

After it I see exception in logcat:

02-22 09:51:34.171    8014-8048/com.jetico.bestcrypt I/LiveAuthClient﹕ No refresh token available, sorry!
02-22 09:51:34.171    8014-8048/com.jetico.bestcrypt I/LiveAuthClient﹕ All tokens expired, you need to call login() to initiate interactive logon
02-22 09:51:34.172    8014-8048/com.jetico.bestcrypt E/MSAAuthenticator$5[onAuthComplete] - 262﹕ Was unable to connect to the MSA login session
com.onedrive.sdk.authentication.ClientAuthenticatorException: Was unable to connect to the MSA login session
        at com.onedrive.sdk.authentication.MSAAuthenticator$5.onAuthComplete(MSAAuthenticator.java:260)
        at com.microsoft.services.msa.LiveAuthClient$3.doInBackground(LiveAuthClient.java:484)
        at com.microsoft.services.msa.LiveAuthClient$3.doInBackground(LiveAuthClient.java:473)
        at android.os.AsyncTask$2.call(AsyncTask.java:292)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

Code in "catch" and "finally" is never called too.

Would you be so kind to give us a FULL working code sample of Activity with synchronous auth process?

@daboxu
Copy link
Contributor

daboxu commented Feb 23, 2016

hi @isabsent , could you please give me more information about the OneDrive Android SDK you are using? I tested with this sample app and could not reproduce the issue. I am testing this explorer app with OneDrive sdk v1.1.3.

@isabsent
Copy link
Author

I have enclosed two demo projects (converted from LearnAndroid) with hidden credentials.
First - [LearnAndroid_Async.zip](https://github.com/OneDrive/onedrive-explorer-
android/files/142061/LearnAndroid_Async.zip) is asynchronous and works fine.
Second - LearnAndroid_Sync.zip is synchronous and hangs up.

Above mentioned error log is not reproduced in this simplified project, but I see other error message in logcat

02-23 02:00:48.627 14150-14311/com.nextlabs.learnandroid I/LiveAuthClient: No refresh token available, sorry!
02-23 02:00:48.627 14150-14311/com.nextlabs.learnandroid I/LiveAuthClient: All tokens expired, you need to call login() to initiate interactive logon
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314: Failed silent login, interactive login required
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314: com.onedrive.sdk.authentication.ClientAuthenticatorException: Failed silent login, interactive login required
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at com.onedrive.sdk.authentication.MSAAuthenticator$5.onAuthComplete(MSAAuthenticator.java:312)
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at com.microsoft.services.msa.LiveAuthClient$3.doInBackground(LiveAuthClient.java:484)
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at com.microsoft.services.msa.LiveAuthClient$3.doInBackground(LiveAuthClient.java:473)
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-23 02:00:48.632 14150-14311/com.nextlabs.learnandroid E/MSAAuthenticator$5[onAuthComplete] - 314:     at java.lang.Thread.run(Thread.java:818)

in both (async and sync) cases.

@daboxu
Copy link
Contributor

daboxu commented Feb 24, 2016

Hi @isabsent , thanks for the code and finally I figured it out. In our SDK, we assume it should be run in a background so then we can let you continue to the right page. I changed your code in OneDrive.java:onCreate() begins from line86 like this:

            try {
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        OneDriveClient.Builder builder = new OneDriveClient.Builder().fromConfig(createConfig());
                        builder.loginAndBuildClient(Onedrive.this/*, callback*/);    //last called
                        System.out.println();      //is never called
                        return null;
                    }
                }.execute();
            } catch (Exception e){
                e.printStackTrace();       //is never called
            } finally {
                btnLink.setEnabled(true);  //is never called
            }

and it works. Please let me know if it helps.

@isabsent
Copy link
Author

As I understand it is not a synchronous call, because

btnLink.setEnabled(true);

may be called BEFORE token was retrieved?

@daboxu
Copy link
Contributor

daboxu commented Feb 24, 2016

@isabsent yes you are right. It is a work around for now. Thanks for reporting it and we will fix it soon.

@isabsent
Copy link
Author

Thanks! Please, take a look on this issue too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants