AppAuth-Android
AppAuth-Android copied to clipboard
Obtaining authorization code with startActivityForResult approach not working prior Android 5.0
Performing an authorization request in versions prior 5.0 using startActivityForResult approach is returning RESULT_CANCELED back immediately to the caller Activity due to a known limitation (this SO thread explain it very well)
I've managed to handle this case by calling AuthorizationService.performAuthorizationRequest() as described in README.md
AuthorizationService authService = new AuthorizationService(this); authService.performAuthorizationRequest( authRequest, PendingIntent.getActivity(this, 0, new Intent(this, MyAuthCompleteActivity.class), 0), PendingIntent.getActivity(this, 0, new Intent(this, MyAuthCanceledActivity.class), 0));
MyAuthCompleteActivity can be your original authorization activity, then handle intent (getIntent()) the same way you would handle onActivityResult intent data.
Apologies for this; after we added this functionality, we realized the current implementation of it will work on Android 5.0+ only as you state. I believe there is a way we can adapt the implementation to work on all supported versions of Android, so I plan to do that once I can resume contributing code to the project.
Hello, Thanx for the lib, very useful. I have the same problem, do you plan to fix this issue soon ?
I can't give a timeline for a fix, in the meantime I recommend sticking to using a PendingIntent for completion / cancelation as this works across all supported versions.
On Android 7, a single request will work normally but if I start the authorization activity (it's a singleTask) while it's already running then I observe the same behavior: immediate onActivityResult with RESULT_CANCELED .. Any workarounds?
Could you please add note about this bug in Readme ? Have wasted a day debugging and blaming my code.
If the bug was fixed, maybe we can remove @TargetApi(Build.VERSION_CODES.LOLLIPOP) from getAuthorizationRequestIntent now? It is a bit confusing.
It looks to me like the startActivityForResult flow only works because of a "hack" that Google added to Android 5.0. This comment appears in the source code (regarding the use of "new task" launch modes, like singleTask:
// For whatever reason this activity is being launched into a new
// task... yet the caller has requested a result back. Well, that
// is pretty messed up ...
I'm not that well-versed in the different intent types, but it doesn't seem logical to me to have sign-in happen on a separate task. Wouldn't that make it possible for the user to switch back to the previous activity while also having the login still open? Seems confusing at least.
In any case, I find the original performAuthorizationRequest flow works as expected and has no compatibility issues. I also prefer that I can process the response in a separate Activity, rather than having the host one have to forward the response to my user-session module.
At a minimum, I think the Readme docs should make the compatibility issues clear, and mention the trade-offs in using the startActivityForResult flow. I can try to prepare a PR for this, if there is agreement, although I'm not sure I understand the issue well enough yet to provide a full explanation.