5

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.

Namek
  • 477
  • 1
  • 8
  • 20
  • Please add also examples about your OAuth2Authenticator calls as you are asking what you are doing wrong it that one. And Xamarin.Auth works for me for Dropbox authentication. For Microsoft Live, I had to do some modifications as there is some slight deviation in the handling of the requests compared to what is implemented in Xamarin.Auth. Also add some information which version of Xamarin.Auth you are using. – eX0du5 Jul 08 '16 at 09:12
  • For me it's quiet simple to understand, what could be wrong, after some research. My backend OAuth2 implementation required Header with *Authorization* key and hashed value. The problem is, that Xamarin.Auth is not able to pass header - https://github.com/xamarin/Xamarin.Auth/issues/61 Is there any workaround? – Namek Jul 08 '16 at 11:17
  • which branch of this project are you using? The outdated master branch oder https://github.com/xamarin/Xamarin.Auth/tree/portable-bait-and-switch which also seems to be the basis of the Nuget Package (version 1.5 if I remember correctly). – eX0du5 Jul 13 '16 at 11:41
  • I have the same question. How to use OAuth2Authenticator with username and password instead of clientid and secret ? – Henrik Aug 30 '16 at 10:13

1 Answers1

1

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.

maddhew
  • 54
  • 2