Xamarin.Auth with Google

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(

  • SuchithSuchith INMember ✭✭

    even i got the same problem...:(, any work around,plz post here

  • SuchithSuchith INMember ✭✭

    @tohwei‌ good explanation it helped me to het authenticated by the google. how did you managed to get email id.

  • SuchithSuchith INMember ✭✭

    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

  • AndrewDavisAndrewDavis USMember

    I had the same error as the OP, but following the solution I'm not getting a 400 error now.

    "Error: redirect_uri_mismatch"

  • AndrewDavisAndrewDavis USMember

    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

  • AndrewDavisAndrewDavis USMember

    Ok, got it fixed by adding: clientSecret and accessTokenUrl: new Uri("https://accounts.google.com/o/oauth2/token"),

  • IndishIndish INMember
    edited December 2014

    @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 ?

  • RickGrossRickGross USMember, Beta

    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

  • KB.2300KB.2300 USMember

    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.

  • ChethanChethan USMember ✭✭

    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

  • wibblybibblywibblybibbly GBMember
    edited August 2015

    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?

  • SweeHengCheahSweeHengCheah USMember

    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.

  • hvaughanhvaughan USMember ✭✭✭
    edited September 2016

    @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):

    Xamarin.Auth.OAuth2Authenticator auth = new Xamarin.Auth.OAuth2Authenticator(
        "560741372047-t00000000000v4vf0nvp0o7aqbu94.apps.googleusercontent.com",
        "sHqEPlKg0000000000iP1x",
        "openid email",
        new System.Uri("https://accounts.google.com/o/oauth2/auth"),
        new System.Uri("https://mysite.domain.net/"),
        new System.Uri("https://accounts.google.com/o/oauth2/token"));
    
  • LizticDevLizticDev USMember ✭✭

    Using an embedded User-Agend (WebView) is now blocked by google.

  • Avi669Avi669 INMember ✭✭

    So oauth for google is not possible using xamarin.auth ?

  • bohrbohr USMember

    @Avi669 said:
    So oauth for google is not possible using xamarin.auth ?

    Someone knows ? That would be annoying to use Xamarin.auth, if so.

  • bohrbohr USMember

    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/ ?

  • AhirAhir USMember ✭✭

    Any solution for OAuth Google without webviews

  • hvaughanhvaughan USMember ✭✭✭

    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 a WebView). 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

  • moljacmoljac HRBeta ✭✭✭

    @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

  • hvaughanhvaughan USMember ✭✭✭
    edited May 7

    @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.

  • UdhayaKumarUdhayaKumar USMember

    "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.

  • hvaughanhvaughan USMember ✭✭✭
  • UdhayaKumarUdhayaKumar USMember

    @hvaughan I have already implemented this and worked well before.
    But Unfortunately this service is being blocked by google.

  • moljacmoljac HRBeta ✭✭✭

    @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.

  • TomPeerTomPeer USMember ✭✭
    edited May 18

    @moljac said:
    @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?

  • moljacmoljac HRBeta ✭✭✭

    @TomPeer

    I don't understand how this works.

    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

    I upgraded Xamarin.Auth from 1.2... to 1.5 Alpha 15 and set UsingNativeUI to true.

    You did the right thing.

    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.

    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.

    Whats wrong?

    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).

    What Xamarin understand under "Native UI"?

    "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:

    I think not a Chrome instance?

    It is. And not only Chrome - to complicate everything even more. But read the docs and see you in slack channel

  • TomPeerTomPeer USMember ✭✭

    Thank you for your answer. I think I have to learn complete new ... Thank you Google! :(

  • suelintonsuelinton BRMember

    @moljac said:
    @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

    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;

    2. The "ResolveLibraryProjectImports" task failed unexpectedly.
    

    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, ICollection1 jars, ICollection1 resolvedResourceDirectories, ICollection1 resolvedAssetDirectories, ICollection1 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?

  • moljacmoljac HRBeta ✭✭✭

    @suelinton ComicBook sample is your starting point in that repo.

  • suelintonsuelinton BRMember
    edited June 27

    @moljac said:
    @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!

  • suelintonsuelinton BRMember
    edited October 13

    @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?).

Sign In or Register to comment.