I'm developing an application using Xamarin.Android.
I need to enable login with Facebook and Google and I choose to use the Xamarin.Auth component.
I got it work with facebook but I have some issue with Google.
This is my code:
var auth = new OAuth2Authenticator ( clientId: "myId.apps.googleusercontent.com", clientSecret: "mysecred", scope: "openid", authorizeUrl: new Uri ("https://accounts.google.com/o/oauth2/auth"), redirectUrl: new Uri ("myredirect:oob"), accessTokenUrl: new Uri ("https://accounts.google.com/o/oauth2/token"), getUsernameAsync: null ); activity.StartActivity (auth.GetUI (activity)); auth.Completed += (sender, e) => { Console.WriteLine (e.IsAuthenticated); };
Is this way the Google Activity is displayed, and I can insert my Username and password. But when I click the google login button I get a message like this:
google auth please copy this code switch to your application and paste it there [code]
What I have to do with that code? I just need to get the user name/lastname/email/id.
Thanks a lot!
Posts
Same problem(
even i got the same problem...:(, any work around,plz post here
@alberto.8709 @AlexanderSamuilov @SuchithM
What I have to do with that code?
First, let me answer the above question…
This is useful when you develop an app on limited UI environment / Or Not web based. Example: Printer with little Lcd screen. For instance, Brother printer use this to ask u to authenticate via specific website first, then copy the code to printer, so that you can print/scan doc to google drive or vise verse.
Right, how I solve it? ...
After a long…. investigation. This is mainly affected/determined by what you put on your redirectUrl.
If you apply the redirectUrl assigned by google, example:
Then you will see message "blar,,..blar,.. successfully authentication".
But, Auth.Completed will not be fired.
**Using own defined UrlType will not work either. Dun know why. **
However, change your redirectURL to a valid URL (a live website) will solve the problem.
Make sure you have the valid url, plus what you have defined in google app must be identical with the one you put in mobile app.
No place to put callbackURL? ..hmmm...
Instead of using IOS app type's client Id, please change it to web-application type. (This is the trick!).
Give me a 'Like' if this help
@tohwei good explanation it helped me to het authenticated by the google. how did you managed to get email id.
Bellow steps worked for me.
1.register in google developer console as webapplication
2.on authentication complete it will return access_token
3.by using the access_token make the REST request to get user complete information (https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessTokenValue + “.)
4.Deserialize the json response to get information in object.
check the source code : http://stackoverflow.com/questions/25520180/google-account-login-integration-for-android-xamarin/25766256#25766256
I had the same error as the OP, but following the solution I'm not getting a 400 error now.
"Error: redirect_uri_mismatch"
I found my way to this explanation of the error.
http://stackoverflow.com/questions/11485271/google-oauth-2-authorization-error-redirect-uri-mismatch
In my developer console the URI listed is: http://localhost, and there is no option to edit the list and add another url. With redirectUrl: new Uri("http://localhost"), I get a new error now.
Error: unsupported_response_type
Ok, got it fixed by adding: clientSecret and accessTokenUrl: new Uri("https://accounts.google.com/o/oauth2/token"),
@AndrewDavis Is it android app ? If yes, how did you add the clientSecret and accessTokenURl. I mean they are readonly fields. There is no constructor for them in android. Can you share your code ?
Jeff,
How does one do this: "Instead of using IOS app type's client Id, please change it to web-application type. (This is the trick!)." I don't see this option on my Developer Console - at least I can't find the option.
Thanks,
Rick
Hi,
Is there any update on the issue. I am facing the following errors:
1. unsupported_response_type - when I use the Client ID for the installed apps in Google Developer console.
2. I followed one of the suggestions and used web app related client id and secret. However, the app is presenting me the redirect url (the page at this url) and is not firing the Completed Event. Instead, it fires Error event and the detail shows a 400 Bad Request !!
Any help is appreciated.
Redirect uri should be same as that registered in google developer console. and it will not allow "http://localhost"
For registration steps and implementation in detail follow this http://appliedcodelog.blogspot.in/2015/08/login-by-google-account-integration-for.html
How long should it take to program an App, that lets the user login with google (oauth) and takes them to a landing screen. The app has only one screen to see once logged in. I am interested to know what you think as to how long it takes to build the APK so a person can login and then land on the private screen.
Any ideas please?
For installed app, you should not be adding the client_secret and token_url, because that will cause a screen to pop-up in your app to copy and past the token code. I can't get this to work despite trying all the solutions here.
The solution provided by @SuchithM does not work either, I get redirect_uri_mismatch. The only Uri required by my app is either http://localhost or *:oob. But these Uri will cause a different kind of problem.
@SweeHengCheah This is working fine for me. Xamarin Auth uses the passed in redirect URL to know when to pop out of the WebView. Google uses the redirect URL to go to after a successful login. So in theory, after you successfully login, Google looks up your redirect URL, forwards you there, then the Xamarin Auth WebView sees that you have hit that redirect URL and pops you out of the WebView.
That means you should be able to use the same redirect URL when initializing Xamarin Auth as you the one you entered in the Google Developer Console site when you created your OAuth 2 credentials (the one under Authorized redirect URIs).
I initialize Xamarin Auth on iOS like so (specifying the secret and token end point might not be necessary but does not hurt anything for me at least, also I added https://mysite.domain.net/ into the Google Developer Console site under Authorized redirect URIs):
Using an embedded User-Agend (WebView) is now blocked by google.
So oauth for google is not possible using xamarin.auth ?
Someone knows ? That would be annoying to use Xamarin.auth, if so.
Yep, I confirm : http://www.theregister.co.uk/2016/08/23/google_to_block_web_views_from_using_its_oauth/
So we now need to implement platform specific login in android, ios and w8 projects ?
Google says " [...] pushing the OAuth support to whatever browser is present on the device means the login can persist on that device [...]" but how to implement this ? https://www.nuget.org/packages/Google.Apis/ ?
Any solution for OAuth Google without webviews
Xamarin.Auth has a new versions that use the Chrome Custom Tabs on Android (which is not the same as a
WebView
) and the Safari controller on iOS (which again is not the same as aWebView
). Xamarin.Auth is currently still working on issues with Xamarin Forms usage for this latest version though.https://github.com/xamarin/Xamarin.Auth/issues/150#issuecomment-284055926
@hvaughan
That post/comment is quite old.
Thanks for helping out by replacing me and responding. Maybe you can try new 1.5.0-alpha-12. Android and iOS Forms support is done, trying to add UWP and other, but facing some issues.
In https://github.com/moljac/Xamarin.Auth.Samples.NugetReferences try ./XamarinForms/Evolve16Labs/ComicBook.sln
@moljac
Very nice thank you! I will check them out. Very excited for the new version. Really appreciate all the work you're doing on the library.
"This user-agent is not permitted to make an OAuth authorization request to Google as it is classified as an embedded user-agent (also known as a web-view). Per our policy, only browser are permitted to make authorization requests to Google. We offer several libraries and samples for native apps to perform authorization request in the browser."
Since google has restricted the gmail login for xamarin is not working now..
Anyone having solution for this?
Thanks in advance.
@UdhayaKumar Did you read the post directly above mine...?
https://forums.xamarin.com/discussion/comment/271636/#Comment_271636
@hvaughan I have already implemented this and worked well before.
But Unfortunately this service is being blocked by google.
@UdhayaKumar Please READ my post @hvaughan pointed out too.
Service is blocked by google for NEW apps using EMBEDDED WEBVIEWS. Xamarin.Auth supports Native UI since version 1.4.0 (2017-03). So read, do not copy/paste text from google. We are aware of that.
I don't understand how this works. I upgraded Xamarin.Auth from 1.2... to 1.5 Alpha 15 and set UsingNativeUI to true. Now its open a external Chrome instead of using internal WebView, I can login and then ... it ends on my success page, configured in my Google Account. Whats wrong? What Xamarin understand under "Native UI"? I think not a Chrome instance?
@TomPeer
It is not trivial. I ll try to explain, based on your questions, but go and read docs (work in progress - I'm the single dev on this lib)
https://github.com/xamarin/Xamarin.Auth/blob/portable-bait-and-switch/component/GettingStarted.md
And rest - details (WARNING WORK IN PROGRESS):
https://github.com/xamarin/Xamarin.Auth/tree/portable-bait-and-switch/docs
You did the right thing.
Here - code smell. Though no code, but it smells.
"Success page"? In new OAuth by google (actually in old too, but it was allowed) we (all of us) created Web App in google console and this was wrong. Web/Server apps are for servers where sensitive data is not that easy accessible like in mobile apps or browser apps. Success page was some web page (URL - redirect_url) which existed and was loadable by browser (embedded WebView in Xamarin.Auth). This was restriction imposed by Xamarin.Auth due to the implementation. Xamarin.Auth did not allow redirect_urls like "http://localhost/succes.html" or "some-scheme:/oauth2redirect".
With new google oauth you cannot use http scheme! Why? Browser will load redirect_url and you will never be able to parse it to get OAuth data. You need custom scheme for App/Deep linking to be "returned" to your app.
Read GettingStarted.md and light will shine. There are links to community slack channel where we (10+ people that implemented new Xamarin.Auth OAuth and myself) can help you.
Quite a bit (from your description). Read the docs, try the samples and then ask.
Problems? Yes they are, but mostly nuget related (seems that order of adding nuget packages and their version can cause troubles).
"Native UI" is not term coined by Xamarin. It is mentioned in RFC. True it is confusing and each provider coins its own term like "Installed apps" (google). In general - you shell out to system browser (Chrome on Android and Safari on iOS) via API which has reduced surface for security reasons on Android CustomTabs (Chrome CustomTabs) and on iOS SFSafariViewController. In this out of process (process meaning your app) user does login/authentication and then returns via App/Deep link[s] back to the process (your app).
If you want "pleasure" read the RFC and other docs where Native is mentioned:
It is. And not only Chrome - to complicate everything even more. But read the docs and see you in slack channel
Thank you for your answer. I think I have to learn complete new ... Thank you Google!
I had problems with that repo, the problems are:
**1. The name 'InitializeComponent' does not exist in the current context Samples.NativeUI ~\NativeUI\Samples.NativeUI\App.xaml.cs;
System.IO.FileNotFoundException: Could not load assembly 'Samples.NativeUI, Version=0.0.0.0, Culture=neutral, PublicKeyToken='. Perhaps it doesn't exist in the Mono for Android profile?
File name: 'Samples.NativeUI.dll'
at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference reference, ReaderParameters parameters)
at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(String fullName, ReaderParameters parameters)
at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(String fullName)
at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.GetAssembly(String fileName)
at Xamarin.Android.Tasks.ResolveLibraryProjectImports.Extract(DirectoryAssemblyResolver res, ICollection
1 jars, ICollection
1 resolvedResourceDirectories, ICollection1 resolvedAssetDirectories, ICollection
1 resolvedEnvironments)at Xamarin.Android.Tasks.ResolveLibraryProjectImports.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext() Samples.NativeUI.Android **
Someone can you help me?
@suelinton ComicBook sample is your starting point in that repo.
Ok functioned very well, though, just when using the Opera Browser on Android, the Google API returned the error message "disallowed_useragent". On Chrome worked perfectly.
P.S. I'm sorry, I did not know the Cookbook is the correct repo. Thanks!
@moljac
I can't run the google authentication just on iOS. He just stay on Authenticator Page, whithout starts the navigator. Do you can help me?
P.S. Tests performed on IPhone Simulator(Can be this the problem?).
If u want google auth without webview u can use this https://developer.xamarin.com/samples/xamarin-forms/WebServices/OAuthNativeFlow/