Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Xamarin.Auth for OAuth2 authentication - username and password?

I'm facing some issue, while using Xamarin.Auth for OAuth2 authentication.

From POSTMAN I'm sending request for token, via GET method to my backend URL: http://example.com/backend/oauth/token by adding to header:

Authorization, Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

and with below parameters in URL:

client_id=backendClient

scope=read

grant_type=password

username=admin

password=admin

so it goes like: http://example.com/backend/oauth/token?client_id=backendClient&scope=read&grant_type=password&username=admin&password=admin

it gives me back JSON, which looks like:

{
"access_token": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"token_type": "bearer",
"refresh_token": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"expires_in": 9999,
"scope": "read"
}

I wanted to authenticate to my backend service, from mobile application (using Xamarin.Forms app), and I've tried to use Xamarin.Auth, for authenticating to my back-end - https://developer.xamarin.com/guides/xamarin-forms/web-services/authentication/oauth/

Indeed using OAuth2Authenticator, it's able at least to "ping" my backend (by putting URL, clientIde etc), as it returns:

<UnauthorizedException>
    <error>unauthorized</error>
    <error_description>Full authentication is required to access this resource</error_description>
</UnauthorizedException>

however message it's beeing send in wrong way - at least I think so, as there is no Authorization in header + I haven't see any possibility to pass username and password into OAuth2Authenticator.

Does anyone have idea, how I can do this? Or should I use something else - not Xamarin.Auth?

Thanks.

like image 851
Namek Avatar asked Nov 09 '22 12:11

Namek


1 Answers

Here's how I do it (not using Xamarin.Auth):

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenGoesHere);

Then you can add any content you like and Get/Post/PutAsync, whatever you need.

like image 163
maddhew Avatar answered Nov 15 '22 12:11

maddhew