internal static async Task PerformCodeExchangeAsync(string code, string codeVerifier) { var accessToken = await GetAccessToken(code, codeVerifier); using (var httpClient = new HttpClient(new HttpClientHandler { AllowAutoRedirect = true })) { httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); var userInfoResponse = await httpClient.GetAsync(USER_INFO_END_POINT); var userInfoResponseContent = await userInfoResponse.Content.ReadAsStringAsync(); var account = JsonConvert.DeserializeObject <JObject>(userInfoResponseContent); var user = new User { Token = accessToken }; if (!userInfoResponseContent.Contains("error")) { user.FamilyName = account["family_name"].Value <string>(); user.GivenName = account["given_name"].Value <string>(); user.Email = account["email"].Value <string>(); user.Name = account["name"].Value <string>(); user.Id = account["sub"].Value <string>(); user.Picture = account["picture"].Value <string>(); } await UserSignedIn.Raise(user); } }
public static void Initilize(string clientId) { if (clientId.IsEmpty()) { Log.For(typeof(Google)).Error("Please set the ClientId by calling Initilize method first!"); return; } Auth = new Xamarin.Auth.OAuth2Authenticator( clientId, "", "openid profile email", new Uri(AUTH_END_POINT), new Uri($"com.googleusercontent.apps.{clientId.Remove(".apps.googleusercontent.com")}:/oauth2redirect"), new Uri(TOKEN_END_POINT), null, true) { AllowCancel = true }; Auth.Completed += async(s, args) => { if (args.IsAuthenticated) { var request = new Xamarin.Auth.OAuth2Request("GET", new Uri(USER_INFO_END_POINT), null, args.Account); var response = await request.GetResponseAsync(); var accountStr = await response.GetResponseTextAsync(); var account = JsonConvert.DeserializeObject <JObject>(accountStr); await UserSignedIn.Raise(new Google.User { FamilyName = account["family_name"].Value <string>(), GivenName = account["given_name"].Value <string>(), Email = account["email"].Value <string>(), Name = account["name"].Value <string>(), Id = account["sub"].Value <string>(), Picture = account["picture"].Value <string>(), Token = args.Account.Properties["id_token"] ?? "" }); } }; }
public static void Initialize() { UIRuntime.OnActivityResult.Handle(args => { if (args.Item1 == SIGNIN_REQUEST_CODE && args.Item2 == Android.App.Result.Ok) { if (args.Item3 == null) { Log.For(typeof(Google)).Error("[Zebble.Google] => The Google Play Services are not installed on your device, please make sure to installed them"); return; } var result = Auth.GoogleSignInApi.GetSignInResultFromIntent(args.Item3); if (result.IsSuccess) { var account = result.SignInAccount; UserSignedIn.Raise(new User { FamilyName = account.FamilyName, GivenName = account.GivenName, Id = account.Id, Name = account.DisplayName, Picture = account.PhotoUrl?.ToString(), Email = account.Email, Token = account.IdToken }); } } }); var context = UIRuntime.CurrentActivity; var serverClientIdStr = context.Resources.GetIdentifier("server_client_id", "string", context.PackageName); if (serverClientIdStr == 0) { Log.For(typeof(Google)).Error("Google Client ID is not set on Android application. Please add server_client_id to the resource string file."); return; } var clientId = context.GetString(serverClientIdStr); var gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn) .RequestEmail() .RequestProfile() .RequestIdToken(clientId) .Build(); ApiClient = new GoogleApiClient.Builder(UIRuntime.CurrentActivity) .AddApi(Auth.GOOGLE_SIGN_IN_API, gso) .AddConnectionCallbacks(connectedCallback: bundle => { if (bundle != null) { Log.For(typeof(Google)).Debug("Google connected"); } else { Log.For(typeof(Google)).Error("Google connection filed"); } }).Build(); ApiClient.Connect(); }