2016-04-21 69 views
1

我刚开始使用OneDrive API及其附带的示例程序(OneDriveApiBrowser)。我如何在没有看到权限屏幕的情况下登录到OneDrive(在初始时间之后)

正如预期的那样,我第一次登录时(使用“登录到MSA ...”,我被要求提供凭据,我的双因素代码,最后是一个权限屏幕,询问我是否同意访问该应用程序想要我的OneDrive帐户

但是,退出程序并重新启动后,我没有登录。我重复“登录到MSA ...”,我不再提示输入凭据(如我所料),但我上午与权限画面再次提示。

是否有具有应用程序日志的归途中没有永远prompti请求用户许可?

为了学习如何使用OneDrive API,我只使用了微软作为API的一部分提供的示例代码,其位于https://github.com/OneDrive/onedrive-sdk-csharp/tree/master/samples/OneDriveApiBrowser。代码不能直接从那里下载,但是在这个项目的根源,https://github.com/OneDrive/onedrive-sdk-csharp。这将下载API的源代码以及示例代码和单元测试。

回答

2

做一些更多的闲逛之后,我终于找到了如何做到这一点。我的解释将在上面原始问题中提到的示例程序的上下文中。

在节目中,在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); 
} 
0

应用程序需要使用wl.offline_access作用域来保存用户同意信息并在没有UI提示的情况下刷新访问令牌。

有关,你可以在你的应用程序使用的范围,更多详细信息请参见https://dev.onedrive.com/auth/msa_oauth.htm#authentication-scopes

+0

OK,但该范围已经存在...'私人静态只读的String []斯科普斯= { “onedrive.readwrite”,“WL .offline_access“,”wl.signin“};'。另外,我正在使用C#SDK而不是直接使用https界面,所以我不确定如何将该文档翻译成C#API框架。 – Jim

+0

您能否提供有关如何创建OneDrive客户端设置和Authenticate调用配置的详细信息/代码? –

+0

当然,我只是使用API​​的示例代码。我编辑了我的OP,并详细说明了它可以找到的位置。在这一点上,它只是“股票代码”,没有任何变化...... – Jim

相关问题