做一些更多的閒逛之後,我終於找到了如何做到這一點。我的解釋將在上面原始問題中提到的示例程序的上下文中。
在節目中,在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