我如何登录OneDrive(在初始时间之后)而没有看到权限屏幕 [英] How do I login to OneDrive (after the initial time) without seeing the permissions screen

查看:191
本文介绍了我如何登录OneDrive(在初始时间之后)而没有看到权限屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用OneDrive API及其附带的示例程序(OneDriveApiBrowser).

I have just started working with the OneDrive API and the sample program that comes with it (OneDriveApiBrowser).

如预期的那样,我第一次登录(使用登录到MSA ...")时,系统要求我输入凭据,我的2因子代码,最后是一个权限屏幕,询问我是否同意该访问权限应用程序要使用我的OneDrive帐户.

As expected, the first time I logged in (using "Sign In to MSA...", I was asked for credentials, my 2-factor code, and finally a permissions screen asking if I approve of the access that the app wants against my OneDrive account.

但是,退出程序并重新启动程序后,我没有登录.我重复进入登录MSA ...",并且不再提示输入凭据(如我所料),但是我<再次在权限屏幕上提示strong> am .

But, after I exit the program and restart it, I am not logged in. I repeat going to "Sign In to MSA..." and I am no longer prompted for credentials (as I expected), but I am prompted with the permissions screen again.

是否有一种方法可以使应用程序重新登录而又不总是提示用户许可?

Is there a way of having the app log back in without always prompting the user for permission?

要了解如何使用OneDrive API,我仅使用Microsoft作为该API的一部分提供的示例代码,位于 https://github.com/OneDrive/onedrive-sdk-csharp .这将下载API的源代码以及示例代码和单元测试.

For learning how to use the OneDrive API, I am just using the sample code that Microsoft supplied as part of the API located at https://github.com/OneDrive/onedrive-sdk-csharp/tree/master/samples/OneDriveApiBrowser. The code can't be downloaded directly from there, but at the root of this project, https://github.com/OneDrive/onedrive-sdk-csharp. This will download the source for the API as well as the sample code and unit tests.

推荐答案

经过一番摸索之后,我终于找到了解决方法.我的解释是在上面原始问题中提到的示例程序的背景下进行的.

After doing some more poking around, I finally found out how to do this. My explanation here will be in the context of the sample program mentioned in the original question above.

在程序的SignIn方法中,已完成一些设置,包括调用OneDriveClient.GetMicrosoftAccountClient(...),然后调用以下代码:

In the program, in the SignIn method, there was some setup being done which included calling OneDriveClient.GetMicrosoftAccountClient(...), then calling the following:

if (!this.oneDriveClient.IsAuthenticated)
{
    await this.oneDriveClient.AuthenticateAsync();
}

因此,需要完成两件事.我们需要保存上面代码的结果,然后将RefreshToken值保存在安全的地方...(这是一个很长的字符串).

So, two things needed to be done. We need the save the result from the code above, then save the RefreshToken value somewhere safe... (It's just a very long string).

if (!this.oneDriveClient.IsAuthenticated)
{
    AccountSession accountSession = await this.oneDriveClient.AuthenticateAsync();
    // Save accountSession.RefreshToken somewhere safe...
}

最后,我需要在OneDriveClient.GetMicrosoftAccountClient(...)调用周围放置一个if,并且仅在尚未保存已保存的刷新令牌(或由于添加到注销调用中的代码而被删除了)时才调用它.如果我们拥有已保存的刷新令牌,则调用`OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(...).完成后,整个SignIn方法看起来像这样.

Finally, I needed to put an if around the OneDriveClient.GetMicrosoftAccountClient(...) call and only call it if the saved refresh token has not been saved yet (or as been deleted due to code added to the logout call...) If we have a saved refresh token, we call `OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(...) instead. The entire SignIn method looks like this when I am done.

private async Task SignIn(ClientType clientType)
{
    string refreshToken = null;
    AccountSession accountSession;

    // NOT the best place to save this, but will do for an example...
    refreshToken = Properties.Settings.Default.RefreshToken;

    if (this.oneDriveClient == null)
    {
        if (string.IsNullOrEmpty(refreshToken))
        {
            this.oneDriveClient = clientType == ClientType.Consumer
                        ? OneDriveClient.GetMicrosoftAccountClient(
                            FormBrowser.MsaClientId,
                            FormBrowser.MsaReturnUrl,
                            FormBrowser.Scopes,
                            webAuthenticationUi: new FormsWebAuthenticationUi())
                        : BusinessClientExtensions.GetActiveDirectoryClient(FormBrowser.AadClientId, FormBrowser.AadReturnUrl);
        }
        else
        {
            this.oneDriveClient = await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(FormBrowser.MsaClientId,
                    FormBrowser.MsaReturnUrl,
                    FormBrowser.Scopes, 
                    refreshToken);
        }
    }

    try
    {
        if (!this.oneDriveClient.IsAuthenticated)
        {
            accountSession = await this.oneDriveClient.AuthenticateAsync();
            // NOT the best place to save this, but will do for an example...
            Properties.Settings.Default.RefreshToken = accountSession.RefreshToken;
            Properties.Settings.Default.Save();
        }

        await LoadFolderFromPath();

        UpdateConnectedStateUx(true);
    }
    catch (OneDriveException exception)
    {
        // Swallow authentication cancelled exceptions
        if (!exception.IsMatch(OneDriveErrorCode.AuthenticationCancelled.ToString()))
        {
            if (exception.IsMatch(OneDriveErrorCode.AuthenticationFailure.ToString()))
            {
                MessageBox.Show(
                    "Authentication failed",
                    "Authentication failed",
                    MessageBoxButtons.OK);

                var httpProvider = this.oneDriveClient.HttpProvider as HttpProvider;
                httpProvider.Dispose();
                this.oneDriveClient = null;
            }
            else
            {
                PresentOneDriveException(exception);
            }
        }
    }
}

为完整起见,我更新了退出代码

For completeness, I updated the logout code

private async void signOutToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (this.oneDriveClient != null)
    {
        await this.oneDriveClient.SignOutAsync();
        ((OneDriveClient)this.oneDriveClient).Dispose();
        this.oneDriveClient = null;

        // NOT the best place to save this, but will do for an example...
        Properties.Settings.Default.RefreshToken = null;
        Properties.Settings.Default.Save();
    }

    UpdateConnectedStateUx(false);
}

这篇关于我如何登录OneDrive(在初始时间之后)而没有看到权限屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆