做一些更多的闲逛之后,我终于找到了如何做到这一点。我的解释将在上面原始问题中提到的示例程序的上下文中。
在节目中,在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...
}
最后,我需要把一个if
围绕OneDriveClient.GetMicrosoftAccountClient(...)
呼叫,只调用它,如果保存刷新令牌尚未保存(或者被删除,由于代码添加到注销调用...)如果我们的保存了刷新标记,则我们调用`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);
}
来源
2016-04-21 20:40:25
Jim
OK,但该范围已经存在...'私人静态只读的String []斯科普斯= { “onedrive.readwrite”,“WL .offline_access“,”wl.signin“};'。另外,我正在使用C#SDK而不是直接使用https界面,所以我不确定如何将该文档翻译成C#API框架。 – Jim
您能否提供有关如何创建OneDrive客户端设置和Authenticate调用配置的详细信息/代码? –
当然,我只是使用API的示例代码。我编辑了我的OP,并详细说明了它可以找到的位置。在这一点上,它只是“股票代码”,没有任何变化...... – Jim