使用Xamarin Android App中的OneDrive SDK进行身份验证

时间:2016-01-06 04:46:27

标签: android xamarin onedrive

我在Cross Plattform应用程序中使用onedrive SDK。在Windows上,身份验证通过OneDriveClientExtensions.GetClientUsingWebAuthenticationBroker工作。

现在我正在尝试登录Android。我试着这样做了:

oneDriveClient = OneDriveClient.GetMicrosoftAccountClient(
                    appId: MSA_CLIENT_ID,
                    returnUrl: RETURN_URL,
                    scopes: scopes,
                    clientSecret: MSA_CLIENT_SECRET);
                await oneDriveClient.AuthenticateAsync();

但是得到一个错误,即没有收到有效的令牌。我是否必须从显示oauth浏览器的WebAuthenticationBrokerAuthenticationProvider实现自己的AuthenticationProvider?或者是什么方式去这里?

1 个答案:

答案 0 :(得分:0)

我使用Xamarin Auth组件解决了这个问题。下面是使用登录名调用webview的代码:

        private const string RETURN_URL = @"https://login.live.com/oauth20_desktop.srf";

private void ShowWebView()
    {
        var auth = new OAuth2Authenticator(
                clientId: MSA_CLIENT_ID,
                scope: string.Join(",", scopes),
                authorizeUrl: new Uri(GetAuthorizeUrl()),
                redirectUrl: new Uri(RETURN_URL));

        auth.Completed += (sender, eventArgs) =>
        {
            if (eventArgs.IsAuthenticated)
            {
                //Do Something
            }
        };

        var intent = auth.GetUI(Application.Context);
        intent.SetFlags(ActivityFlags.NewTask);

        Application.Context.StartActivity(intent);
    }

    private string GetAuthorizeUrl()
    {
        var requestUriStringBuilder = new StringBuilder();
        requestUriStringBuilder.Append("https://login.live.com/oauth20_authorize.srf");
        requestUriStringBuilder.AppendFormat("?{0}={1}", Constants.Authentication.RedirectUriKeyName, RETURN_URL);
        requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ClientIdKeyName, MSA_CLIENT_ID);
        requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ScopeKeyName,
            string.Join("%20", scopes));
        requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ResponseTypeKeyName,
            Constants.Authentication.TokenResponseTypeValueName);

        return requestUriStringBuilder.ToString();
    }