Using Xamarin.Auth for OAuth2 Authentication - Username and Password?

I ran into some problem when using Xamarin.Auth authentication for OAuth2.

From POSTMAN I send a request for a token using the GET method to my backend address: http://example.com/backend/oauth/token adding to the header:

Authorization, basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

and with the options below in the URL:

client_id = backendClient

Volume = read

grant_type = password

= admin username

= admin password

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

it returns me a JSON that looks like this:

{ "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 in my backend service, from a mobile application (using the Xamarin.Forms application), and I tried to use Xamarin.Auth for authentication on my back-end - https://developer.xamarin.com/guides/xamarin -forms / web-services / authentication / oauth /

Indeed, using the OAuth2Authenticator, it can at least ping my backend (by placing the URL, clientIde, etc.) when it returns:

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

however, this message was sent incorrectly - at least, I think so, because there is no authorization in the header. I do not see the ability to pass the username and password to OAuth2Authenticator.

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

Thanks.

+5
source share
1 answer

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

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

You can then add whatever content you like and Get / Post / PutAsync, no matter what you need.

+1
source

All Articles