예제 #1
0
            public async Task <bool> SignOut(string serverClientId)
            {
                try {
                    var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                                     .RequestIdToken(serverClientId)
                                     .RequestServerAuthCode(serverClientId)
                                     .RequestEmail();

                    var gso = gsoBuilder.Build();

                    googleApiClient = new GoogleApiClient.Builder(CurrentActivity)
                                      .AddConnectionCallbacks(this)
                                      .AddOnConnectionFailedListener(this)
                                      .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                                      .Build();
                    googleApiClient.Connect();
                    await connectedTask.Task;
                    var result = await Auth.GoogleSignInApi.SignOut(googleApiClient).AsAsync <Statuses> ();

                    return(true);
                } finally {
                    googleApiClient?.UnregisterConnectionCallbacks(this);
                    googleApiClient?.Disconnect();
                }
                //var result = await Auth.GoogleSignInApi.SignOut (googleApiClient).AsAsync<ResultS> ();
                //result.
            }
예제 #2
0
            public async Task <GoogleSignInResult> Authenticate(string serverClientId, params string[] scopes)
            {
                var googleScopes = scopes?.Select(s => new Scope(s))?.ToArray();

                var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                                 .RequestIdToken(serverClientId)
                                 .RequestEmail()
                ;

                //if (googleScopes != null && googleScopes.Any())
                //  gsoBuilder = gsoBuilder.RequestScopes(googleScopes.First(), googleScopes.Skip(1)?.ToArray());


                var gso = gsoBuilder.Build();

                var activity = CurrentActivity;

                googleApiClient = await new GoogleApiClient.Builder(activity)
                                  .EnableAutoManage(activity, this)
                                  .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                                  .BuildAndConnectAsync();

                var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(googleApiClient);

                if (tcsSignIn != null && !tcsSignIn.Task.IsCompleted)
                {
                    tcsSignIn.TrySetCanceled();
                }

                tcsSignIn = new TaskCompletionSource <GoogleSignInResult>();

                activity.StartActivityForResult(signInIntent, SIGN_IN_REQUEST_CODE);

                return(await tcsSignIn.Task);
            }
예제 #3
0
        internal GoogleClientManager()
        {
            if (CurrentActivity == null)
            {
                throw new GoogleClientNotInitializedErrorException(GoogleClientBaseException.ClientNotInitializedErrorMessage);
            }

            var gopBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                             .RequestEmail();

            if (!string.IsNullOrWhiteSpace(_serverClientId))
            {
                gopBuilder.RequestServerAuthCode(_serverClientId, false);
            }

            if (!string.IsNullOrWhiteSpace(_clientId))
            {
                gopBuilder.RequestIdToken(_clientId);
            }

            foreach (var s in _initScopes)
            {
                gopBuilder.RequestScopes(new Scope(s));
            }

            GoogleSignInOptions googleSignInOptions = gopBuilder.Build();

            // Build a GoogleSignInClient with the options specified by gso.
            mGoogleSignInClient = GoogleSignIn.GetClient(CurrentActivity, googleSignInOptions);
        }
예제 #4
0
        public void Logout()
        {
            var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn).RequestEmail();

            GoogleSignIn.GetClient(_context, gsoBuilder.Build())?.SignOut();

            _googleApiClient.Disconnect();
        }
예제 #5
0
            public async Task <GoogleSignInResult> Authenticate(GoogleAuthenticator authenticator)
            {
                var activity = CurrentActivity;

                CheckGooglePlayServices(activity);

                try
                {
                    var googleScopes = authenticator.Scope?.Select(s => new Scope(s))?.ToArray();
                    var clientID     = GoogleAuthenticator.GetGoogleClientId(authenticator.ClientId);
                    var serverId     = GoogleAuthenticator.GetGoogleClientId(authenticator.ServerClientId);
                    var gsoBuilder   = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                                       .RequestEmail();

                    if (serverId != null)
                    {
                        gsoBuilder.RequestIdToken(serverId);
                    }

                    if (authenticator.Scope != null)
                    {
                        foreach (var scope in authenticator.Scope)
                        {
                            gsoBuilder.RequestScopes(new Scope(scope));
                        }
                    }

                    var gso = gsoBuilder.Build();

                    googleApiClient = new GoogleApiClient.Builder(activity)
                                      .AddConnectionCallbacks(this)
                                      .AddOnConnectionFailedListener(this)
                                      .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                                      .Build();
                    googleApiClient.Connect();

                    var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(googleApiClient);

                    if (tcsSignIn != null && !tcsSignIn.Task.IsCompleted)
                    {
                        tcsSignIn.TrySetCanceled();
                    }

                    tcsSignIn = new TaskCompletionSource <GoogleSignInResult>();

                    activity.StartActivityForResult(signInIntent, SIGN_IN_REQUEST_CODE);

                    var success = await tcsSignIn.Task;
                    return(success);
                }
                finally
                {
                    googleApiClient?.UnregisterConnectionCallbacks(this);
                    googleApiClient?.Disconnect();
                }
            }
예제 #6
0
        public void SignIn()
        {
            verifyInit();
            Task.Run(async() =>
            {
                try
                {
                    var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                                     .RequestIdToken(_clientId)
                                     .RequestEmail();

                    var gso          = gsoBuilder.Build();
                    _googleApiClient = new GoogleApiClient.Builder(_context)
                                       .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                                       .Build();
                    _googleApiClient.Connect();

                    if (signInResultSource != null && !signInResultSource.Task.IsCompleted)
                    {
                        signInResultSource.TrySetCanceled();
                    }

                    signInResultSource = new TaskCompletionSource <GoogleSignInResult>();

                    var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(_googleApiClient);
                    _context.StartActivityForResult(signInIntent, RC_SIGN_IN);
                    var result = await signInResultSource.Task;
                    if (result == null || result.Status.IsCanceled || result.Status.IsInterrupted)
                    {
                        _callbackDelegate.OnConnectionFailed("Connection cancelled");
                        return;
                    }

                    if (!result.IsSuccess)
                    {
                        _callbackDelegate.OnConnectionFailed(result.Status.StatusMessage);
                        return;
                    }
                    _accountName = result.SignInAccount?.DisplayName;
                    _idToken     = result.SignInAccount?.IdToken;

                    _callbackDelegate.OnConnectionSucceeded();
                }
                catch (Exception ex)
                {
                    _callbackDelegate.OnConnectionFailed(ex.Message);
                }
            });
        }
예제 #7
0
        internal GoogleClientManager()
        {
            if (CurrentActivity == null)
            {
                GoogleClientErrorEventArgs errorEventArgs = new GoogleClientErrorEventArgs();
                Exception exception = null;

                errorEventArgs.Error   = GoogleClientErrorType.SignInInternalError;
                errorEventArgs.Message = GoogleClientBaseException.SignInInternalErrorMessage;
                exception = new GoogleClientSignInInternalErrorException();

                _loginTcs.TrySetException(exception);
            }

            var gopBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                             .RequestEmail();

            if (!string.IsNullOrWhiteSpace(_serverClientId))
            {
                gopBuilder.RequestServerAuthCode(_serverClientId, false);
            }

            if (!string.IsNullOrWhiteSpace(_clientId))
            {
                gopBuilder.RequestIdToken(_clientId);
            }

            foreach (var s in _initScopes)
            {
                gopBuilder.RequestScopes(new Scope(s));
            }

            GoogleSignInOptions googleSignInOptions = gopBuilder.Build();

            var googleApiClientBuilder = new GoogleApiClient.Builder(Application.Context)
                                         .AddConnectionCallbacks(this)
                                         .AddOnConnectionFailedListener(this)
                                         .AddApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions);

            foreach (var a in _initApis)
            {
                googleApiClientBuilder.AddApi(a);
            }

            GoogleApiClient = googleApiClientBuilder.Build();
        }
            public async Task <GoogleSignInResult> Authenticate(IGoogleAuthOptions options)
            {
                var googleScopes = options?.Scopes?.Select(s => new Scope(s))?.ToArray();

                var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn);

                if (!string.IsNullOrEmpty(options.ServerClientId))
                {
                    gsoBuilder = gsoBuilder.RequestIdToken(options.ServerClientId);
                }

                if (googleScopes != null && googleScopes.Any())
                {
                    gsoBuilder = gsoBuilder.RequestScopes(googleScopes.First(), googleScopes.Skip(1).ToArray());
                }

                if (options.FetchProfile)
                {
                    gsoBuilder = gsoBuilder.RequestProfile().RequestEmail();
                }

                var gso = gsoBuilder.Build();

                var activity = Plugin.SocialAuth.Droid.SocialAuth.CurrentActivity;

                googleApiClient = new GoogleApiClient.Builder(activity)
                                  .EnableAutoManage(activity, 789, this)
                                  .AddConnectionCallbacks(this)
                                  .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                                  .Build();

                googleApiClient.Connect();

                tcsSignIn = new TaskCompletionSource <GoogleSignInResult>();

                var result = await tcsSignIn.Task;

                googleApiClient.StopAutoManage(activity);

                return(result);
            }
        internal GoogleClientManager()
        {
            if (CurrentActivity == null)
            {
                throw new GoogleClientNotInitializedErrorException(GoogleClientBaseException.ClientNotInitializedErrorMessage);
            }

            var gopBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                             .RequestEmail();

            if (!string.IsNullOrWhiteSpace(_serverClientId))
            {
                gopBuilder.RequestServerAuthCode(_serverClientId, false);
            }

            if (!string.IsNullOrWhiteSpace(_clientId))
            {
                gopBuilder.RequestIdToken(_clientId);
            }

            foreach (var s in _initScopes)
            {
                gopBuilder.RequestScopes(new Scope(s));
            }

            GoogleSignInOptions googleSignInOptions = gopBuilder.Build();

            var googleApiClientBuilder = new GoogleApiClient.Builder(Application.Context)
                                         .AddConnectionCallbacks(this)
                                         .AddOnConnectionFailedListener(this)
                                         .AddApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions);

            foreach (var a in _initApis)
            {
                googleApiClientBuilder.AddApi(a);
            }

            GoogleApiClient = googleApiClientBuilder.Build();
        }
        internal GoogleClientManager()
        {
            var gopBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                             .RequestEmail();

            if (!string.IsNullOrWhiteSpace(_serverClientId))
            {
                gopBuilder.RequestServerAuthCode(_serverClientId, false);
            }

            if (!string.IsNullOrWhiteSpace(_clientId))
            {
                gopBuilder.RequestIdToken(_clientId);
            }

            GoogleSignInOptions googleSignInOptions = gopBuilder.Build();

            GoogleApiClient = new GoogleApiClient.Builder(Application.Context)
                              .AddConnectionCallbacks(this)
                              .AddOnConnectionFailedListener(this)
                              .AddApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
                              .AddScope(new Scope(Scopes.Profile))
                              .Build();
        }
예제 #11
0
        public void Initialize(Activity activity, string serverClientId, bool requestProfile, bool requestEmail)
        {
            var builder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn);

            if (requestProfile)
            {
                builder = builder.RequestProfile();
            }
            if (requestEmail)
            {
                builder = builder.RequestEmail();
            }

            if (!string.IsNullOrEmpty(serverClientId))
            {
                builder = builder.RequestServerAuthCode(serverClientId);
            }

            googleSignInOptions = builder.Build();


            googleSignInClient = GoogleSignIn.GetClient(activity, googleSignInOptions);
            Activity           = activity;
        }
예제 #12
0
            public async Task<GoogleSignInResult> Authenticate(string serverClientId, params string[] scopes)
            {
                var googleScopes = scopes?.Select(s => new Scope(s))?.ToArray();

                var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                                                        .RequestIdToken(serverClientId)
                                                        .RequestEmail ()
                                                        ;

                //if (googleScopes != null && googleScopes.Any())
                  //  gsoBuilder = gsoBuilder.RequestScopes(googleScopes.First(), googleScopes.Skip(1)?.ToArray());


                var gso = gsoBuilder.Build();

                var activity = CurrentActivity;

                googleApiClient = await new GoogleApiClient.Builder(activity)
                                         .EnableAutoManage(activity, this)
                                         .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                                         .BuildAndConnectAsync();

                var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(googleApiClient);

                if (tcsSignIn != null && !tcsSignIn.Task.IsCompleted)
                    tcsSignIn.TrySetCanceled();

                tcsSignIn = new TaskCompletionSource<GoogleSignInResult>();

                activity.StartActivityForResult(signInIntent, SIGN_IN_REQUEST_CODE);

                return await tcsSignIn.Task;
            }