C# Windows Phone Live SDK API-重新启动应用程序后获取新会话对象
因此,我成功地将我的Windows Phone 8应用程序连接到Live API,并成功地从我的hotmail帐户读取数据 我可以访问所需的客户端ID和实时访问令牌 但是,当我退出并重新启动应用程序时,我会丢失对会话和客户端对象的所有引用,我必须重新启动该过程 我不想让用户对web掩码感到恼火,在web掩码中,用户必须再次同意,每次启动应用程序时,他都会向我提供所需的权限。但是我也没有找到一种方法,可以在没有这个步骤的情况下获得对会话对象的引用 登录掩码仅在安装应用程序后第一次显示,之后,仅显示上述屏幕。 但是用户每次都接受这一点仍然很烦人 我已经尝试序列化会话对象,这是不可能的,因为该类没有标准构造函数 也许可以使用live access令牌创建新会话,但我还没有找到这样做的方法 有什么想法吗?我做错了什么,我知道有一种方法可以在不提示用户的情况下再次登录。 我对每一个想法都心存感激 我使用的一些代码:C# Windows Phone Live SDK API-重新启动应用程序后获取新会话对象,c#,windows-phone,C#,Windows Phone,因此,我成功地将我的Windows Phone 8应用程序连接到Live API,并成功地从我的hotmail帐户读取数据 我可以访问所需的客户端ID和实时访问令牌 但是,当我退出并重新启动应用程序时,我会丢失对会话和客户端对象的所有引用,我必须重新启动该过程 我不想让用户对web掩码感到恼火,在web掩码中,用户必须再次同意,每次启动应用程序时,他都会向我提供所需的权限。但是我也没有找到一种方法,可以在没有这个步骤的情况下获得对会话对象的引用 登录掩码仅在安装应用程序后第一次显示,之后,仅显示
/// <summary>
/// This method is responsible for authenticating an user asyncronesly to Windows Live.
/// </summary>
public void InitAuth()
{
this.authClient.LoginCompleted +=
new EventHandler<LoginCompletedEventArgs>(this.AuthClientLoginCompleted);
this.authClient.LoginAsync(someScopes);
}
/// <summary>
/// This method is called when the Login process has been completed (successfully or with error).
/// </summary>
private void AuthClientLoginCompleted(object sender, LoginCompletedEventArgs e)
{
if (e.Status == LiveConnectSessionStatus.Connected)
{
LiveConnector.ConnectSession = e.Session; // where do I save this session for reuse?
this.connectClient = new LiveConnectClient(LiveConnector.ConnectSession);
// can I use this access token to create a new session later?
LiveConnector.LiveAccessToken = LiveConnector.ConnectSession.AccessToken;
Debug.WriteLine("Logged in.");
}
else if (e.Error != null)
{
Debug.WriteLine("Error signing in: " + e.Error.ToString());
}
}
有人知道我如何在重新启动应用程序后访问新会话吗?在花了整整两天的时间搜索我犯的错误后,我终于发现我做错了什么:我必须使用wl.offline\U访问范围来实现这一点 让我在这里引用另一位用户的话: “如果您的应用程序使用wl.offline\u访问范围,则live:SignInButton控件将为您保存并自动加载。只需使用SessionChanged事件捕获会话对象。这样,用户只需登录一次。” (见附件)
现在一切又变得有趣了。真不敢相信这就是问题所在。测试和工作。很好 我自己一直在努力让它在Windows Live+Azure移动服务应用程序上正常工作,所以我想现在我可以在这里发布一个完整的工作代码示例了 关键部分是wl.U访问范围和对InitializeAsync的调用 注意:此示例还连接到Windows Azure移动服务。如果你不使用MobileService,只需删除与MobileService相关的内容即可
private static LiveConnectSession _session;
private static readonly string[] scopes = new[] {"wl.signin", "wl.offline_access", "wl.basic"};
private async System.Threading.Tasks.Task Authenticate()
{
try
{
var liveIdClient = new LiveAuthClient("YOUR_CLIENT_ID");
var initResult = await liveIdClient.InitializeAsync(scopes);
_session = initResult.Session;
if (null != _session)
{
await MobileService.LoginWithMicrosoftAccountAsync(_session.AuthenticationToken);
}
if (null == _session)
{
LiveLoginResult result = await liveIdClient.LoginAsync(scopes);
if (result.Status == LiveConnectSessionStatus.Connected)
{
_session = result.Session;
await MobileService.LoginWithMicrosoftAccountAsync(result.Session.AuthenticationToken);
}
else
{
_session = null;
MessageBox.Show("Unable to authenticate with Windows Live.", "Login failed :(", MessageBoxButton.OK);
}
}
}
finally
{
}
}
+1为此,我也遇到了同样的问题,谢谢!!
private static LiveConnectSession _session;
private static readonly string[] scopes = new[] {"wl.signin", "wl.offline_access", "wl.basic"};
private async System.Threading.Tasks.Task Authenticate()
{
try
{
var liveIdClient = new LiveAuthClient("YOUR_CLIENT_ID");
var initResult = await liveIdClient.InitializeAsync(scopes);
_session = initResult.Session;
if (null != _session)
{
await MobileService.LoginWithMicrosoftAccountAsync(_session.AuthenticationToken);
}
if (null == _session)
{
LiveLoginResult result = await liveIdClient.LoginAsync(scopes);
if (result.Status == LiveConnectSessionStatus.Connected)
{
_session = result.Session;
await MobileService.LoginWithMicrosoftAccountAsync(result.Session.AuthenticationToken);
}
else
{
_session = null;
MessageBox.Show("Unable to authenticate with Windows Live.", "Login failed :(", MessageBoxButton.OK);
}
}
}
finally
{
}
}