OAuth Xamarin- |
, , OAuth, . , OAuth - ( 2-3 ) , URL. ( ) WebView, (token ) URL.
, OAuth: , Mail.ru, Dropbox, Foursquare, GitHub, Instagram, LinkedIn, Microsoft, Slack, SoundCloud, Visual Studio Online, Trello.
, OAuth Xamarin Xamarin.Auth, :
Xamarin.Auth . , .
Xamarin.Auth Nuget, Xamarin Components .
, SDK Facebook . , PCL . OAuth , PCL Xamarin.Auth.
Xamarin.Auth Xamarin.Forms.OAuth Bruno Bernardo. Xamarin, .
OAuth Microsoft. https://apps.dev.microsoft.com Client ID ( ).
PCL IOAuthService , .
public interface IOAuthService
{
Task Login();
void Logout();
}
, , DependencyService.Get
DependencyService.Get
.
OAuth-. Login()
Logout()
providerName
( string
, int
enum
) .
, Xamarin.Auth Nuget , iOS Android. IOAuthService
Dependency
.
OAuth2Authenticator
:
var auth = new OAuth2Authenticator
(
clientId: "_CLIENT_ID",
scope: "wl.basic, wl.emails, wl.photos",
authorizeUrl: new Uri("https://login.live.com/oauth20_authorize.srf"),
redirectUrl: new Uri("https://login.live.com/oauth20_desktop.srf"),
clientSecret: null,
accessTokenUrl: new Uri("https://login.live.com/oauth20_token.srf")
)
{
AllowCancel = true
};
:
auth.Completed += AuthOnCompleted;
, - , auth.GetUI()
. iOS :
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(auth.GetUI(), true, null);
Android Xamarin.Forms :
Forms.Context.StartActivity(auth.GetUI(Forms.Context));
AuthOnCompleted()
, iOS ( Android ):
UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(true, null);
(access_token
expires_in
)
var token = authCompletedArgs.Account.Properties["access_token"];
var expireIn = Convert.ToInt32(authCompletedArgs.Account.Properties["expires_in"]);
var expireAt = DateTimeOffset.Now.AddSeconds(expireIn);
, email . Xamarin.Auth OAuth2Request
.
var request = new OAuth2Request("GET", new Uri("https://apis.live.net/v5.0/me"), null, account);
var response = await request.GetResponseAsync();
JSON , .
if (response.StatusCode == HttpStatusCode.OK)
{
var userJson = response.GetResponseText();
var jobject = JObject.Parse(userJson);
result.LoginState = LoginState.Success;
result.Email = jobject["emails"]?["preferred"].ToString();
result.FirstName = jobject["first_name"]?.ToString();
result.LastName = jobject["last_name"]?.ToString();
result.ImageUrl = jobject["picture"]?["data"]?["url"]?.ToString();
var userId = jobject["id"]?.ToString();
result.UserId = userId;
result.ImageUrl = $"https://apis.live.net/v5.0/{userId}/picture";
}
, . , URL . , expires_in
( ).
auth.Error
, .
. Xamarin.Forms, Xamarin iOS/Android. :
https://bitbucket.org/binwell/login
!