Forum Libraries, Components, and Plugins

Finding a Google OAuth2 Component

I've been trying to use Xamarin.Auth for my cross-service authentication into my app. I've got FB setup and it was fairly easy, but as people have identified in this thread, there is no support for Google's 2 callback mechanisms without modifying the library. Does anyone know of any components or libraries that work out of the box for both Facebook and Google?



  • Tim -

    I don't recall where I found this so I don't take credit, but it did take me a great deal of searching to get the Google OAuth2 running.

    I've probably mangled the Intent results here but I needed to have my calling Activity wait until the entire Login process was completed and not just the initial authentication, since I'm also pulling in some user credentials.

    The code is basically the same as for Facebook so you can use the same component - there's just about zero documentation on calling into Google.

    Good luck!

    private Context _context;
    private Configuration _config;
    public override string ToString() {
    return "GooglePlus";
    public override void Login(Context context, Configuration appConfig) {
    _context = context;
    _config = appConfig;
    OAuth2Authenticator auth = new OAuth2Authenticator(
        clientId: "",
        scope: "openid email profile",
        authorizeUrl: new Uri(""),
        redirectUrl: new Uri(""),
        getUsernameAsync: null);
    auth.AllowCancel = true;
    auth.Completed += LoginCompleted;
    Intent oauthLogin = auth.GetUI(context);
    private void LoginCompleted(object sender, AuthenticatorCompletedEventArgs e) {
    var request = new OAuth2Request("GET", new Uri(""), null, e.Account);
    request.GetResponseAsync().ContinueWith(t => {
        if (t.IsFaulted) {
        string s = t.Exception.Flatten().InnerException.ToString();
        Log.Error(AppConfig.APPLICATION_NAME, "Error logging into Google [{0}]", s);
        _config.UsersProfile.IsAuthenticated = false;
        else if (t.IsCanceled) {
        _config.UsersProfile.IsAuthenticated = false;
        Log.Info(AppConfig.APPLICATION_NAME, "Google login cancelled by user");
        else {
        var obj = JObject.Parse(t.Result.GetResponseText());
        string username = (string)obj["name"];
        string email = (string)obj["email"];
        _config.UsersProfile.DisplayName = username;
        _config.UsersProfile.Email = email;
        _config.UsersProfile.IsAuthenticated = true;
        Log.Info(AppConfig.APPLICATION_NAME,"Logged into Google [{0}] [{1}]", _config.UsersProfile.Email, _config.UsersProfile.DisplayName);
        _config.UsersProfile.UserId = GetUserId(email, username, this.ToString());
        ((Activity)_context).StartActivityForResult(new Intent(), 400); 
    }, UIScheduler);
    private static readonly TaskScheduler UIScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Sign In or Register to comment.