且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

初始授权后,OneDrive自动登录

更新时间:2023-12-04 10:09:10

//您的代码

_msaAuthenticationProvider = new MsaAuthenticationProvider("XXXX", "https://login.live.com/oauth20_desktop.srf", new[] {"onedrive.readonly", "wl.signin", "wl.offline_access" });
await _msaAuthenticationProvider.AuthenticateUserAsync();
_oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", _msaAuthenticationProvider);
await _msaAuthenticationProvider.AuthenticateUserAsync();

//添加此
//保存刷新令牌

// add this
// save refresh token

var refreshtoken = (((MsaAuthenticationProvider)oneDriveClient.AuthenticationProvider).CurrentAccountSession).RefreshToken;

//在会话之间安全地存储此刷新令牌.
//------------------------------------
//稍后,如果您想连接到OneDrive,请创建AccountSession并使用存储的RefreshToken

// store this refresh token secure between sessions.
// ------------------------------------
// later, if you want to connect to OneDrive, create AccountSession and use that stored RefreshToken

AccountSession session = new AccountSession();
session.ClientId = <<your id>>; // your "XXXX"
session.RefreshToken = refreshtoken;
_msaAuthenticationProvider = new MsaAuthenticationProvider(....
_oneDriveClient = new OneDriveClient(....
_msaAuthenticationProvider.CurrentAccountSession = session;
await _msaAuthenticationProvider.AuthenticateUserAsync();