2016-07-07 252 views
5

我在使用Xamarin.Auth进行OAuth2身份验证时遇到了一些问题。使用Xamarin.Auth进行OAuth2身份验证 - 用户名和密码?

从邮差我送的令牌请求,通过GET方法我的后端网址:通过向头 http://example.com/backend/oauth/token

授权,基本xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

,并与下面的参数网址:

client_id = backendClient

范围=读

grant_type =密码

用户名=管理员

密码=管理员

所以它是这样: http://example.com/backend/oauth/token?client_id=backendClient&scope=read&grant_type=password&username=admin&password=admin

它给我回JSON ,看起来像:

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

我想验证我的后端服务,从移动应用程序(使用Xamarin.Forms应用程序),我已经尝试过使用Xamarin.Auth,用于验证到我的后端 - https://developer.xamarin.com/guides/xamarin-forms/web-services/authentication/oauth/

实际上使用OAuth2Authenticator,它至少能够 “平” 我的后端(通过把URL,clientIde等),因为它返回:

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

然而,它是beeing发送错误的方式 - 至少我认为是这样,因为没有授权在标题+我还没有看到任何可能性传递用户名和密码到OAuth2Authenticator

有没有人有想法,我该怎么做?或者我应该使用别的东西 - 不是Xamarin.Auth?

谢谢。

+0

请添加关于您的OAuth2Authenticator调用的示例,因为您在问自己做错了那一个。 Xamarin.Auth适用于我的Dropbox身份验证。对于Microsoft Live,我不得不做一些修改,因为与Xamarin.Auth中实现的请求相比,处理请求时有一些细微的偏差。 还要添加一些信息,您正在使用哪种版本的Xamarin.Auth。 – eX0du5

+0

对我来说,在一些研究之后,它很简单易懂,可能是错的。 我的后端OAuth2实现需要包含* Authorization *键和哈希值的Header。 问题是,Xamarin.Auth无法通过标题 - https://github.com/xamarin/Xamarin.Auth/issues/61 有什么解决方法吗? – Namek

+0

你使用哪个分支的这个项目?过时的主分支或者https://github.com/xamarin/Xamarin.Auth/tree/portable-bait-and-switch,这似乎也是Nuget Package的基础(版本1.5,如果我没有记错的话)。 – eX0du5

回答

1

这里是我如何做到这一点(不使用Xamarin.Auth):

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

然后你就可以添加你喜欢的任何内容和GET/POST/PutAsync,无论你需要什么。

相关问题