Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sample-XamarinAuth-Google/Xamarin_GoogleAuth/Authentication/GoogleAuthenticator.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
61 lines (52 sloc)
2.1 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Xamarin.Auth; | |
namespace Xamarin_GoogleAuth.Authentication | |
{ | |
public class GoogleAuthenticator | |
{ | |
private const string AuthorizeUrl = "https://accounts.google.com/o/oauth2/v2/auth"; | |
private const string AccessTokenUrl = "https://www.googleapis.com/oauth2/v4/token"; | |
private const bool IsUsingNativeUI = true; | |
private OAuth2Authenticator _auth; | |
private IGoogleAuthenticationDelegate _authenticationDelegate; | |
public GoogleAuthenticator(string clientId, string scope, string redirectUrl, IGoogleAuthenticationDelegate authenticationDelegate) | |
{ | |
_authenticationDelegate = authenticationDelegate; | |
_auth = new OAuth2Authenticator(clientId, string.Empty, scope, | |
new Uri(AuthorizeUrl), | |
new Uri(redirectUrl), | |
new Uri(AccessTokenUrl), | |
null, IsUsingNativeUI); | |
_auth.Completed += OnAuthenticationCompleted; | |
_auth.Error += OnAuthenticationFailed; | |
} | |
public OAuth2Authenticator GetAuthenticator() | |
{ | |
return _auth; | |
} | |
public void OnPageLoading(Uri uri) | |
{ | |
_auth.OnPageLoading(uri); | |
} | |
private void OnAuthenticationCompleted(object sender, AuthenticatorCompletedEventArgs e) | |
{ | |
if (e.IsAuthenticated) | |
{ | |
var token = new GoogleOAuthToken | |
{ | |
TokenType = e.Account.Properties["token_type"], | |
AccessToken = e.Account.Properties["access_token"] | |
}; | |
_authenticationDelegate.OnAuthenticationCompleted(token); | |
} | |
else | |
{ | |
_authenticationDelegate.OnAuthenticationCanceled(); | |
} | |
} | |
private void OnAuthenticationFailed(object sender, AuthenticatorErrorEventArgs e) | |
{ | |
_authenticationDelegate.OnAuthenticationFailed(e.Message, e.Exception); | |
} | |
} | |
} |