2016-04-21 71 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

相關問題