Last active
November 19, 2018 15:18
-
-
Save lewixlabs/6addc59de22318400a3a9691078ecb33 to your computer and use it in GitHub Desktop.
Xamarin OAuth2 Custom Class to get & use refresh_token
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 System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Xamarin.Auth; | |
namespace MyNetStandardLibrary.OAuth2 | |
{ | |
// inspired by https://lostechies.com/jimmybogard/2014/11/13/mobile-authentication-with-xamarin-auth-and-refresh-tokens/ | |
public class CustomOAuth2Authenticator : OAuth2Authenticator | |
{ | |
public CustomOAuth2Authenticator(string clientId, string scope, Uri authorizeUrl, Uri redirectUrl, GetUsernameAsyncFunc getUsernameAsync = null, bool isUsingNativeUI = false) : base(clientId, scope, authorizeUrl, redirectUrl, getUsernameAsync, isUsingNativeUI) | |
{ | |
} | |
public CustomOAuth2Authenticator(string clientId, string clientSecret, string scope, Uri authorizeUrl, Uri redirectUrl, Uri accessTokenUrl, GetUsernameAsyncFunc getUsernameAsync = null, bool isUsingNativeUI = false) : base(clientId, clientSecret, scope, authorizeUrl, redirectUrl, accessTokenUrl, getUsernameAsync, isUsingNativeUI) | |
{ | |
} | |
async public override Task<Uri> GetInitialUrlAsync(Dictionary<string, string> custom_query_parameters = null) | |
{ | |
var initilUrl = await base.GetInitialUrlAsync(custom_query_parameters); | |
return new Uri($"{initilUrl}&access_type=offline"); | |
} | |
async public Task<Dictionary<string,string>> RequestValidAccessTokenAsync(string refreshToken) | |
{ | |
var queryValues = new Dictionary<string, string> | |
{ | |
{"refresh_token", refreshToken}, | |
{"client_id", this.ClientId}, | |
{"grant_type", "refresh_token"} | |
}; | |
if (!string.IsNullOrEmpty(this.ClientSecret)) | |
queryValues["client_secret"] = this.ClientSecret; | |
var accountProperties = await this.RequestAccessTokenAsync(queryValues); | |
this.OnRetrievedAccountProperties(accountProperties); | |
return new Dictionary<string,string>(accountProperties); | |
//return this.RequestAccessTokenAsync(queryValues).ContinueWith(result => | |
//{ | |
// var accountProperties = result.Result; | |
// this.OnRetrievedAccountProperties(accountProperties); | |
// return int.Parse(accountProperties["expires_in"]); | |
//}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment