Xamarin and Auth0 - getting refresh tokens

Viewed 274

I was following the guide provided by auth0 and have been authenticating just fine, but I am getting tired of having to log in everytime I open the app and wanted to start storing and taking advantage of refresh tokens. However I can't seem to get a refresh token, its always null.

In my LoginActivity I have the following

_client = new Auth0Client(new Auth0ClientOptions
        {
            Domain = Resources.GetString(Resource.String.auth0_domain),
            ClientId = Resources.GetString(Resource.String.auth0_client_id),
            //Scope = "offline_access",
            Activity = this
        });

and handling the log in like so

_authorizeState = await _client.PrepareLoginAsync(new { audience = "myaudience.blahblahblah"});


 protected override async void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);

        var loginResult = await _client.ProcessResponseAsync(intent.DataString, _authorizeState);

        var sb = new StringBuilder();
        if (loginResult.IsError)
        {
            sb.AppendLine($"An error occurred during login: {loginResult.Error}");
        }
        else
        {
            var mainActivity = new Intent(this, typeof(MainActivity));
            mainActivity.PutExtra("token", loginResult.AccessToken);
            StartActivity(mainActivity);
            Finish();
        }
    }

If I include the scope then I get an error back that the response doesn't contain an identity token. if I don't include I just don't get the refresh token.

1 Answers

For me, the trick were add Scope row as shown below.

The original code:

client = new Auth0Client(new Auth0ClientOptions
{
    Domain = Resources.GetString(Resource.String.auth0_domain),
    ClientId = Resources.GetString(Resource.String.auth0_client_id),
    Activity = this
});

Changed and working one:

client = new Auth0Client(new Auth0ClientOptions
{
    Domain = Resources.GetString(Resource.String.auth0_domain),
    ClientId = Resources.GetString(Resource.String.auth0_client_id),
    Activity = this,
    Scope = "openid offline_access"
});

I tried only with this:

Scope = "offline_access"

But received an error, until the "openid" in the front of it.

Related