c# - Xamarin.Forms Google API Authenticating Users with an Identity Provider -
i still getting used xamarin.forms , on basic level. went through many articles issue, end couldn't resolve it. so...
currently trying add google authentication inside xamarin.forms application, use droid , ios (no wp). far following guide here. using xamarin.auth authenticate google.
here part source code.
private async void googlesheetsbutton_tapped() { string clientid = null; string redirecturi = null; if (device.runtimeplatform == device.ios) { clientid = constants.iosclientid; redirecturi = constants.iosredirecturl; } else if (device.runtimeplatform == device.android) { clientid = constants.androidclientid; redirecturi = constants.androidredirecturl; } var authenticator = new oauth2authenticator( clientid, null, constants.scope, new uri(constants.authorizeurl), new uri(redirecturi), new uri(constants.accesstokenurl), null, true); authenticator.completed += onauthcompleted; authenticator.error += onautherror; authenticationstate.authenticator = authenticator; var presenter = new xamarin.auth.presenters.oauthloginpresenter(); presenter.login(authenticator); }
the problem coming after method complete it's work. after last line:
presenter.login(authenticator);
everything looks alright , debugging following compiler goes out of method without errors, receive exception, can see here. it's "no compatible code running".
here more information regarding source code:
- source of "constants" class used client ids , urls
public static class constants { public static string appname = "...."; // oauth // google login, configure @ https://console.developers.google.com/ public static string iosclientid = "6.....apps.googleusercontent.com"; public static string androidclientid = "6.....apps.googleusercontent.com"; // these values not need changing public static string scope = "https://www.googleapis.com/auth/userinfo.email"; public static string authorizeurl = "https://accounts.google.com/o/oauth2/auth"; public static string accesstokenurl = "https://www.googleapis.com/oauth2/v4/token"; public static string userinfourl = "https://www.googleapis.com/oauth2/v2/userinfo"; // set these reversed ios/android client ids, :/oauth2redirect appended public static string iosredirecturl = "com.googleusercontent.apps.6......h:/oauth2redirect"; public static string androidredirecturl = "com.googleusercontent.apps.6......l:/oauth2redirect"; }
- source of implemented methods on authentication complete/error, in fact still cannot hit because of error
async void onauthcompleted(object sender, authenticatorcompletedeventargs e) { var authenticator = sender oauth2authenticator; if (authenticator != null) { authenticator.completed -= onauthcompleted; authenticator.error -= onautherror; } googleloginuser user = null; if (e.isauthenticated) { var request = new oauth2request("get", new uri(constants.userinfourl), null, e.account); var response = await request.getresponseasync(); if (response != null) { string userjson = await response.getresponsetextasync(); user = jsonconvert.deserializeobject(userjson); } if (_account != null) { _store.delete(_account, constants.appname); } await _store.saveasync(_account = e.account, constants.appname); await displayalert("email address", user.email, "ok"); } } void onautherror(object sender, authenticatorerroreventargs e) { var authenticator = sender oauth2authenticator; if (authenticator != null) { authenticator.completed -= onauthcompleted; authenticator.error -= onautherror; } var message = e.message; }
- source of android mainactivity added
public class mainactivity : formsappcompatactivity { protected override void oncreate(bundle bundle) { tablayoutresource = resource.layout.tabbar; toolbarresource = resource.layout.toolbar; base.oncreate(bundle); forms.init(this, bundle); global::xamarin.auth.presenters.xamarinandroid.authenticationconfiguration.init(this, bundle); mobilebarcodescanner.initialize(application); loadapplication(new app()); } }
- source of urlschemeinterceptoractivity
[activity(label = "customurlschemeinterceptoractivity", nohistory = true, launchmode = launchmode.singletop)] [intentfilter(new[] { intent.actionview }, categories = new[] { intent.categorydefault, intent.categorybrowsable }, dataschemes = new[] { "com.googleusercontent.apps.6......l" }, datapath = "/oauth2redirect")] public class customurlschemeinterceptoractivity : activity { protected override void oncreate(bundle savedinstancestate) { base.oncreate(savedinstancestate); var uri = new uri(intent.data.tostring()); authenticationstate.authenticator.onpageloading(uri); finish(); } }
here main articles went through => link 1, link 2 , link 3, still couldn't resolve issue.
i not sure error comes from, or can can continue debugging resolve issue.
thanks in advance
solution
- change android compiler android 7.0 inside android project properties. screenshot
- make sure inside android manifest target sdk version. screenshot
- update "xamarin.android.*" nuget packages minimum version 25.4.0.1. they're 23.3.0. found problems dependencies on updating it, make manual upload. went , download manually each package , move packages folder. created own package source , give path folder packages , used install downloaded nuget packages. screenshot
- after issue solved.
please update android sdk api 24 or higher , set compile version project. , update referred assemblies custom tabs , dependencies v25.x.x. thats it. should able working.
vijay.
Comments
Post a Comment