2016-07-07 165 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,無論你需要什麼。

相關問題