在 WP7 中存储 LiveConnectSession

编程入门 行业动态 更新时间:2024-10-27 10:24:48
本文介绍了在 WP7 中存储 LiveConnectSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Windows Phone 7 应用上使用 Hotmail API.因此,我必须授予对我的移动应用程序的访问权限.我使用 Microsoft Live Connect API 来做到这一点,这真的很棒首先.我使用以下代码进行身份验证:

string[] requiredScope = { "wl.signin", "wl.calendars_update", "wl.offline_access" };LiveAuthClient auth = new LiveAuthClient(Configuration.ClientID);auth.LoginCompleted += new EventHandler(authInitializeCompletedHandler);auth.LoginAsync(requiredScope);

ClientID 是我从 MS 获得的用于注册我的应用程序的 ID.我的回调看起来(简单)是这样的:

private void authInitializeCompletedHandler(object sender, LoginCompletedEventArgs e){if (e.Status == LiveConnectSessionStatus.Connected){this._session = e.Session;//会话帽 AccessToken 和 RefreshTokenthis._isAuthorized = true;callDelegates();}}

如果该调用成功(并且当前成功,因此有效),我将存储检索到的会话.此会话可用于 LiveConnectClient 构造函数以启用与 MS API 的通信,在我的应用程序中为 Hotmail API.

LiveConnectClient 客户端 = new LiveConnectClient(_session);

我的问题是我想以某种方式保存该会话,以便当应用程序被墓碑化并重新获得焦点时,我不希望用户再次请求许可 - 他已经给了我.我自己发现返回的会话包含 AccessToken、RefreshToken 和 Expires 属性(我知道那是什么,这不是问题).我如何在以后的通话中使用这些信息?Microsoft 的相关文档到此结束,我找不到任何有用的信息.

我发现的 2 个解决方法:

实时连接按钮:不应该是要走的路,因为我不想仅仅显示我的会话变量就显示实时按钮.RESTful API:这需要我完全重写我到目前为止所做的一切,因为一切都依赖于 LiveConnectClient(这使得通信变得非常容易).

但我真的不想使用这种变通方法.我相信/希望有更好的解决方案.

解决方案

貌似这个答案如下

<块引用>

确保您的应用程序同时包含 wl.signin 和 wl.offline_access 作用域.

如果您未使用 SDK 中提供的登录控件,则应始终在应用启动时调用 LiveAuthClient.InitializeAsync.如果 InitializeAsync 未返回,则仅调用 LiveAuthClient.LoginAsync一个有效的会话.

这些步骤将确保您收到刷新令牌,因此无需首次登录后提示登录或同意.

来自:http://social.msdn.microsoft/Forums/en-US/messengerconnect/thread/6df1f31d-a22c-469c-b9f4-5c79e0882701

I want to use the Hotmail API on my Windows Phone 7 App. Therefore, I have to grant access to my mobile app. I do that using the Microsoft Live Connect API, which really works great for the start. I use the following code for authentication:

string[] requiredScope = { "wl.signin", "wl.calendars_update", "wl.offline_access" };
LiveAuthClient auth = new LiveAuthClient(Configuration.ClientID);
auth.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(authInitializeCompletedHandler);
auth.LoginAsync(requiredScope);

ClientID is the ID I got from MS for registering my app. My callback looks (in simple) like this:

private void authInitializeCompletedHandler(object sender, LoginCompletedEventArgs e)
{
    if (e.Status == LiveConnectSessionStatus.Connected)
    {
        this._session = e.Session; // Session hat AccessToken und RefreshToken
        this._isAuthorized = true;
        callDelegates();
    }
}

If that call succeeds (and it currently does, so that works), I store the retrieved Session. This session can be used in the LiveConnectClient constructor to enable the communication with the MS API's, in my app the Hotmail API.

LiveConnectClient client = new LiveConnectClient(_session);

My problem is that I want to save that session somehow, so that when the app is tombstoned and gets its focus back, I don't want the user to ask again for permission - he already gave me that. I have figured out by myself that the returned session contains a AccessToken, RefreshToken and Expires property (I know what that is, that's not the problem). How can I use that information in future calls? Microsoft's documentation about that ends here and I can't find any useful information.

2 workarounds I found:

Live Connect Button: Should not be the way to go, because I don't want to show the Live Button just that I get my session variable back. RESTful API: This would require that I completely rewrite what I have done so far because everything relies on the LiveConnectClient (which makes communication pretty easy).

But I don't really want to use that workarounds. I believe/hope that there is a better solution.

解决方案

Looks like the answer to this is as follows

Make sure you applications include both wl.signin and wl.offline_access scopes.

If you're not using the signin control provided in the SDK, you should always call LiveAuthClient.InitializeAsync when the app starts. Only call LiveAuthClient.LoginAsync if InitializeAsync does not return a valid session.

These steps will ensure you receiving a refresh token so no need to prompt for login or consent after the first time.

From: http://social.msdn.microsoft/Forums/en-US/messengerconnect/thread/6df1f31d-a22c-469c-b9f4-5c79e0882701

这篇关于在 WP7 中存储 LiveConnectSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 11:09:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/753646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LiveConnectSession

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!