-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


OAuth Xamarin-

, 13 2017 . 17:57 +
, Xamarin. SDK Facebook ( ), ( ) OAuth. Twitter, Microsoft Live, Github , . OAuth .




, , OAuth, . , OAuth - ( 2-3 ) , URL. ( ) WebView, (token ) URL.


, OAuth: , Mail.ru, Dropbox, Foursquare, GitHub, Instagram, LinkedIn, Microsoft, Slack, SoundCloud, Visual Studio Online, Trello.


Xamarin.Auth


, OAuth Xamarin Xamarin.Auth, :


  1. ,

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


PCL IOAuthService , .


public interface IOAuthService
{
Task Login();
void Logout();
}

, , DependencyService.Get().Login() DependencyService.Get().Logout() .


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


!




Binwell, Microsoft MVP Xamarin Certified Developer. Nokia Champion Qt Certified Specialist, Xamarin Azure. mobile 2005 , 2008 : Symbian, Maemo, Meego, Windows Mobile, iOS, Android Windows Phone. Medium.

:

Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/332970/


: [1] []
 

:
: 

: ( )

:

  URL