Created
January 22, 2016 06:36
-
-
Save jaganjan/cf5040316a1456094ff4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static class GenericAuthhelper | |
{ | |
/// <summary> | |
/// Gets or sets the handle authentication completed. | |
/// </summary> | |
/// <value>The handle authentication completed.</value> | |
private static EventHandler<AuthenticatorCompletedEventArgs> HandleAuthCompleted { get; set; } | |
/// <summary> | |
/// Gets or sets the handle error handler. | |
/// </summary> | |
/// <value>The handle error handler.</value> | |
private static EventHandler<AuthenticatorErrorEventArgs> HandleErrorHandler { get; set; } | |
/// <summary> | |
/// oes the auth1. | |
/// </summary> | |
/// <param name="consumerKey">The consumer key.</param> | |
/// <param name="consumerSecret">The consumer secret.</param> | |
/// <param name="requestTokenUrl">The request token URL.</param> | |
/// <param name="authorizeUrl">The authorize URL.</param> | |
/// <param name="accessTokenUrl">The access token URL.</param> | |
/// <param name="callbackUrl">The callback URL.</param> | |
/// <returns>Task<AuthticationResult>.</returns> | |
public static async Task<AuthticationResult> OAuth1( | |
string consumerKey, string consumerSecret, | |
Uri requestTokenUrl, Uri authorizeUrl, | |
Uri accessTokenUrl, Uri callbackUrl) | |
{ | |
var authticationResult = new AuthticationResult(); | |
var tcs = | |
new TaskCompletionSource<AuthticationResult>(); | |
var rootViewController = UIApplication.SharedApplication.KeyWindow.RootViewController; | |
var auth = new OAuth1Authenticator( | |
consumerKey, consumerSecret, requestTokenUrl, | |
authorizeUrl, accessTokenUrl, callbackUrl); | |
try | |
{ | |
auth.ShowUIErrors = false; | |
auth.AllowCancel = true; | |
HandleAuthCompleted = (o, e) => | |
{ | |
rootViewController.DismissViewController(true, null); | |
if (auth.HasCompleted && e.IsAuthenticated) | |
{ | |
try | |
{ | |
authticationResult.AuthToken = e.Account.Properties["oauth_token"]; | |
authticationResult.AuthTokenSecret = e.Account.Properties["oauth_token_secret"]; | |
authticationResult.UserId = e.Account.Properties["user_id"]; | |
authticationResult.ScreenName = e.Account.Properties["screen_name"]; | |
authticationResult.Exception = null; //handling api error on ui | |
tcs.TrySetResult(authticationResult); | |
} | |
catch (Exception ex) | |
{ | |
tcs.TrySetResult(new AuthticationResult() { Exception = ex }); | |
Console.WriteLine(ex); | |
} | |
finally | |
{ | |
auth.Completed -= HandleAuthCompleted; | |
} | |
} | |
}; | |
auth.Completed += HandleAuthCompleted; | |
HandleErrorHandler += (sender, e) => | |
{ | |
try | |
{ | |
tcs.TrySetResult(new AuthticationResult() { Exception = e.Exception }); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
} | |
finally | |
{ | |
auth.Error -= HandleErrorHandler; | |
} | |
}; | |
auth.Error += HandleErrorHandler; | |
} | |
catch (Exception ex) | |
{ | |
tcs.TrySetResult(new AuthticationResult() { Exception = ex }); | |
Console.WriteLine(ex); | |
} | |
rootViewController.PresentViewController(auth.GetUI(), true, null); | |
var task = await tcs.Task; | |
return task; | |
} | |
} | |
//Usage | |
public async Task<AuthticationResult> LoginToTwitterAsync() | |
{ | |
var authticationResult = new AuthticationResult(); | |
try | |
{ | |
authticationResult = await GenericAuthhelper.OAuth1(TwitterKeyHelper.GetConsumerKey(), | |
TwitterKeyHelper.GetConsumerSecret(), | |
new Uri("https://api.twitter.com/oauth/request_token"), | |
new Uri("https://api.twitter.com/oauth/authorize"), | |
new Uri("https://api.twitter.com/oauth/access_token"), | |
new Uri("https://mobile.twitter.com")); | |
if (authticationResult == null) return null; | |
} | |
catch (Exception ex) | |
{ | |
if (authticationResult != null) authticationResult.Exception = ex; | |
} | |
return authticationResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment