在 Xamarin Android 应用程序中使用 OneDrive SDK 进行身份验证

Authenticate with OneDrive SDK in Xamarin Android App

我在 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();

但是收到无法接收到有效令牌的错误。我是否必须实现自己的 AuthenticationProvider 继承自 WebAuthenticationBrokerAuthenticationProvider 谁显示 oauth 的浏览器?或者去这里的路是什么?

我使用 Xamarin 身份验证组件解决了这个问题。这是使用登录名调用 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();
    }