我正在尝试创建用于检索OneNote页面的集成测试。当我尝试使用LiveSDK 5.6 LiveAuthClient时,我的编译器甚至没有看到任何名为LiveAuthClient.LoginAsync(strin [])的方法,这个方法在这里有多个答案(例如1)以及{{3 }:|
var authClient = new LiveAuthClient(OneNoteApiConfig.ClientID);
var result = await authClient.InitializeAsync(OneNoteApiConfig.Scopes);
if (result.Status != LiveConnectSessionStatus.Connected)
{
result = await authClient.LoginAsync(Scopes); // method does not exist
}
有什么想法吗?
我确定我错过了一些明显的东西
似乎有一种名为GetLoginUrl()的方法?
我引用了错误的LiveSDK吗?
我安装了LiveSDK 5.6 NuGet包,该项目是.NET 4.5类库项目
答案 0 :(得分:3)
我设法让它工作,但必须使用GetLoginUrl(),而不是使用Selenium使用测试帐户登录到窗口并批准权限请求,然后使用查询字符串中的代码重定向( ..?code = xx.xxx)我用来调用AuthClient.ExchangeAuthCodeAsync,并获得一个包含AccessToken + RefreshToken的Session,对于我可以使用该刷新令牌的所有其他测试:-)因为它有效一年
[Test]
public async void TryAuthoriseWithMsLiveLibrary()
{
var authClient = new LiveAuthClient(OneNoteApiConfig.ClientID);
var result = await authClient.InitializeAsync(OneNoteApiConfig.Scopes);
var getLoginUrl = authClient.GetLoginUrl(OneNoteApiConfig.Scopes);
Console.WriteLine("\r\nGetLoginUrl = " + getLoginUrl);
// New selenium Driver
_driver = new FirefoxDriver();
//Set wait to 2 seconds by defualt
_driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));
// Go to the url provided by auth client
_driver.Navigate().GoToUrl(getLoginUrl);
// Try find the accept button, if automatically directed to the "Grant Permission page", otherwise try find the Login/Passwd and login first
try
{
_driver.FindElementById("idBtn_Accept");
}
catch (NoSuchElementException)
{
var un = _driver.FindElementByName("login"); // Login TextBox
var pw = _driver.FindElementByName("passwd"); // Password TextBox
var signIn = _driver.FindElementByName("SI"); // Sign-In Button
un.SendKeys(OneNoteTestConfig.WindowsLiveUsername);
pw.SendKeys(OneNoteTestConfig.WindowsLivePassword);
signIn.Submit();
}
// Find a click the idBtn_Accept, if not found straight away, will implictly wait 2 seconds as set baove
var button = _driver.FindElementById("idBtn_Accept");
button.Click();
// Try wait until the client is successfully redirected to the url with the code
try
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(5));
wait.Until(d => d.Url.StartsWith("https://login.live.com/oauth20_desktop.srf"));
//example https://login.live.com/oauth20_desktop.srf?code=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&lc=1033
}
catch (WebDriverTimeoutException)
{
Console.WriteLine(_driver.Url);
throw;
}
Console.WriteLine("\r\nUrl = " + _driver.Url);
// Retrieve code from query string (?code=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&...)
var code = MsLiveAuthHelper.GetSessionCodeFromRedirectedUrl(_driver.Url);
code.Should().NotBeNull();
Console.WriteLine("\r\ncode = " + code);
// Try get open a session using retrieved code.
// From my uynderstanding you can only do this once with the code, as it expired after
// and need to use the refresh token from this point, or re-request a code using above procedure
var session = await authClient.ExchangeAuthCodeAsync(code);
session.Should().NotBeNull();
session.AccessToken.Should().NotBeNullOrWhiteSpace();
Console.WriteLine("\r\nLiveConnectSession.AccessToken = " + session.AccessToken);
Console.WriteLine("\r\nLiveConnectSession.AuthenticationToken = " + session.AuthenticationToken);
Console.WriteLine("\r\nLiveConnectSession.Expires = " + session.Expires);
Console.WriteLine("\r\nLiveConnectSession.RefreshToken = " + session.RefreshToken);
}
对于我的其他测试res.RefreshToken
并有一个TestRefreshTokenHandler : IRefreshTokenHandler
,我将其保存在:
public class TestRefreshTokenHandler : IRefreshTokenHandler
{
public Task SaveRefreshTokenAsync(RefreshTokenInfo tokenInfo)
{
return Task.Factory.StartNew(() =>
{
Console.WriteLine("------------------------");
Console.WriteLine("\r\nUserID = " + tokenInfo.UserId);
Console.WriteLine("\r\nRefresh Token = " + tokenInfo.RefreshToken);
Console.WriteLine("\r\n------------------------");
});
}
public Task<RefreshTokenInfo> RetrieveRefreshTokenAsync()
{
Console.WriteLine("RetrieveRefreshTokenAsync()");
return Task.FromResult(new RefreshTokenInfo(OneNoteTestConfig.OneNoteRefreshToken));
}
}
最后在其他测试中:
[Test]
public async void GetNewTokenByRefresh()
{
var authClient = new LiveAuthClient(OneNoteApiConfig.ClientID, new TestRefreshTokenHandler());
var res = (await authClient.InitializeAsync(OneNoteApiConfig.Scopes)).Session;
Console.WriteLine("\r\nLiveConnectSession.AccessToken = " + res.AccessToken);
Console.WriteLine("\r\nLiveConnectSession.AuthenticationToken = " + res.AuthenticationToken);
Console.WriteLine("\r\nLiveConnectSession.Expires = " + res.Expires);
Console.WriteLine("\r\nLiveConnectSession.RefreshToken = " + res.RefreshToken);
res.AuthenticationToken.Should().NotBeNullOrWhiteSpace();
}
希望这有助于某人!
答案 1 :(得分:1)
我不认为使用Live SDK的桌面应用程序支持登录工作流程,只能从Windows应用商店应用程序支持。您可能需要下载到REST API以进行自动化测试。