如何在不看到权限屏幕的情况下登录 OneDrive(在初始时间后)
How do I login to OneDrive (after the initial time) without seeing the permissions screen
我刚刚开始使用 OneDrive API 及其附带的示例程序 (OneDriveApiBrowser)。
正如预期的那样,我第一次登录时(使用 "Sign In to MSA...",我被要求提供凭据、我的双因素代码,最后是一个权限屏幕,询问我是否批准该应用程序想要的访问权限针对我的 OneDrive 帐户。
但是,在我退出程序并重新启动它后,我没有登录。我重复去 "Sign In to MSA..." 并且不再提示我输入凭据(如我所料),但是我 am 再次提示权限屏幕。
有没有办法让应用重新登录而不总是提示用户许可?
为了学习如何使用 OneDrive API,我只是使用 Microsoft 作为位于 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 的 API 的一部分提供的示例代码。这将下载 API 的源代码以及示例代码和单元测试。
应用程序需要 wl.offline_access
范围来保存用户同意信息并在没有 UI 提示的情况下刷新访问令牌。
有关您可以在应用程序中使用的范围的更多详细信息,请参阅 https://dev.onedrive.com/auth/msa_oauth.htm#authentication-scopes
经过一番摸索,我终于找到了如何做到这一点。我在这里的解释将在上面原始问题中提到的示例程序的上下文中进行。
在程序中,在 SignIn
方法中,进行了一些设置,其中包括调用 OneDriveClient.GetMicrosoftAccountClient(...)
,然后调用以下内容:
if (!this.oneDriveClient.IsAuthenticated)
{
await this.oneDriveClient.AuthenticateAsync();
}
因此,需要完成两件事。我们需要保存上面代码的结果,然后将 RefreshToken 值保存在安全的地方......(它只是一个很长的字符串)。
if (!this.oneDriveClient.IsAuthenticated)
{
AccountSession accountSession = await this.oneDriveClient.AuthenticateAsync();
// Save accountSession.RefreshToken somewhere safe...
}
最后,我需要在 OneDriveClient.GetMicrosoftAccountClient(...)
调用周围放置一个 if
并且仅在保存的刷新令牌尚未保存时调用它(或由于代码添加到logout call...) 如果我们有一个保存的刷新令牌,我们调用`OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(...)。完成后整个 SignIn 方法如下所示。
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);
}
}
}
}
为了完整性,我更新了注销代码
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 API 及其附带的示例程序 (OneDriveApiBrowser)。
正如预期的那样,我第一次登录时(使用 "Sign In to MSA...",我被要求提供凭据、我的双因素代码,最后是一个权限屏幕,询问我是否批准该应用程序想要的访问权限针对我的 OneDrive 帐户。
但是,在我退出程序并重新启动它后,我没有登录。我重复去 "Sign In to MSA..." 并且不再提示我输入凭据(如我所料),但是我 am 再次提示权限屏幕。
有没有办法让应用重新登录而不总是提示用户许可?
为了学习如何使用 OneDrive API,我只是使用 Microsoft 作为位于 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 的 API 的一部分提供的示例代码。这将下载 API 的源代码以及示例代码和单元测试。
应用程序需要 wl.offline_access
范围来保存用户同意信息并在没有 UI 提示的情况下刷新访问令牌。
有关您可以在应用程序中使用的范围的更多详细信息,请参阅 https://dev.onedrive.com/auth/msa_oauth.htm#authentication-scopes
经过一番摸索,我终于找到了如何做到这一点。我在这里的解释将在上面原始问题中提到的示例程序的上下文中进行。
在程序中,在 SignIn
方法中,进行了一些设置,其中包括调用 OneDriveClient.GetMicrosoftAccountClient(...)
,然后调用以下内容:
if (!this.oneDriveClient.IsAuthenticated)
{
await this.oneDriveClient.AuthenticateAsync();
}
因此,需要完成两件事。我们需要保存上面代码的结果,然后将 RefreshToken 值保存在安全的地方......(它只是一个很长的字符串)。
if (!this.oneDriveClient.IsAuthenticated)
{
AccountSession accountSession = await this.oneDriveClient.AuthenticateAsync();
// Save accountSession.RefreshToken somewhere safe...
}
最后,我需要在 OneDriveClient.GetMicrosoftAccountClient(...)
调用周围放置一个 if
并且仅在保存的刷新令牌尚未保存时调用它(或由于代码添加到logout call...) 如果我们有一个保存的刷新令牌,我们调用`OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(...)。完成后整个 SignIn 方法如下所示。
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);
}
}
}
}
为了完整性,我更新了注销代码
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);
}