To use this library, only three steps are needed:
The link to each provider's registration page is available in the wiki.
Use Package Manager Console to install the library in your Xamrain.Forms (Portable) project.
PM> Install-Package Xamarin.Forms.OAuth
For instance, in your App.cs file, add your providers:
public App()
{
OAuthAuthenticator.AddPRovider(OAuthProviders.Facebook("FacebookAppId"));
OAuthAuthenticator.AddPRovider(OAuthProviders.Google("GoogleClientId", "RedirectUrlConfiguredInGoogleAppConsole"));
OAuthAuthenticator.AddPRovider(OAuthProviders.Microsoft("MicrosoftClientId", "RedirectUrlConfiguredInMicrosoftDeveloperApp"));
OAuthAuthenticator.AddPRovider(OAuthProviders.Custom(
"Name",
"https://authorizeUlr",
"http://redirectUrl",
"http://accountUrl",
"ClientId",
logo: ImageSource.FromUri("http://logUrl.png"));
// The root page of your application
// ...
}
var authenticationResult = await OAuthAuthenticator.Authenticate();
if (authenticationResult)
{
var providerName = authenticationResult.Account.Provider;
var accountId = authenticationResult.Account.Id;
var accountDisplayName = authenticationResult.Account.DisplayName;
var accessToken = authenticationResult.Account.AccessToken;
// ...
}
else
{
var errorDescription = authenticationResult.ErrorDescription;
// ...
}
If only one provider is used or if a custom provider selection outside this library is used, authentication can be invoked with the intended provider:
var authenticationResult = await OAuthAuthenticator.Authenticate(OAuthProvider.Facebook("FacebookAppId"));
if (authenticationResult)
{
// ...
}
else
{
var errorMessage = authenticationResult.ErrorMessage;
// ...
}
The account in the result can be used to access the provider's result taking care of all the OAuth parameters injection (Post is also available):
// GET
var authenticationResult = await OAuthAuthenticator.Authenticate(OAuthProvider.Facebook("FacebookAppId"));
var resourceString = await authenticationResult.Account.GetResource<string>("https://graph.facebook.com/v2.5/me");
// or
var resourceJObject = await authenticationResult.Account.GetResource<JObject>("https://graph.facebook.com/v2.5/me");
// or
var resourceType = = await authenticationResult.Account.GetResource<ResourceType>("https://graph.facebook.com/v2.5/me");
// or POST
var httpContent = new StringContent("to send to API"); // can be any type deriving from System.Net.Http.HttpContent
var resource = await authenticationResult.Account.PostToreResource<string>("resourceUrl", httpContent);
// same deserialization of response can be handle as above
If the provider supports it and a refresh token was provided, theh access token can be refreshed:
var authenticationResult = await OAuthAuthenticator.Authenticate(OAuthProvider.Facebook("FacebookAppId"));
if (authenticationResult.Account.RefreshesToken
&& authenticationResult.Account.AccessToken.Expires.HasValue
&& authenticationResult.Account.AccessToken.Expires.Value - DateTime.Now < TimeSpan.FromMinutes(5))
{
var response = await result.Account.RefreshToken();
if (response)
{
// if this is reached, result.Account.AccessToken will be updated with new token and expiration DateTime
}
else
{
//Handle error using response.Error and response.ErrorDescription
}
}
//
A set of customizations are provided. Here is their usage.
OAuthAuthenticator.SetProviderSelectText("Pick one:"));
OAuthAuthenticator.SetBackButtonText("Go Back!"));
OAuthAuthenticator.SetProviderButtonBackground(Color.Red));
OAuthAuthenticator.SetProviderButtonTextColor(Color.FromHex("FFFF00")));
OAuthAuthenticator.SetProviderButtonFontSize(38));
Here is the list of the OAuth providers suported out-of-the-box (20):
- Amazon
- Box
- Dropbox
- Foursquare
- GitHub
- Gitter
- Microsoft
- PayPal
- Salesforce
- Slack
- SoundCloud
- StackExchange
- Visual Studio Online
- Meetup
- Trello
These are the features that will be impleneted next: