在 Windows Phone上使用QQConnect OAuth2


QQ互聯OAuth2.0 .NET SDK 發布以及網站QQ登陸示例代碼 這篇文章講述的普通的ASP.NET站點上使用QQ互聯,本篇文章主要介紹在WindowsPhone環境使用QQ互聯OAuth2 SDK,本文的程序改自Google OAuth2 on Windows Phone。QQ互聯的OAuth2和Google 的OAuth2的流程上差不多,QQ互聯的還更簡單一點。

代碼中使用了如下三個類庫:

這些庫都可以通過NuGet包安裝,需要注意的是JSON.NET (4.0.7)目前和RestSharp的最新版本102.6.0.0不兼容,需要使用JSON.NET (4.0.5)版本。

想把這個例子跑起來,需要到http://connect.qq.com 注冊獲取到appkey和appscrect,填寫到配置文件:

image

其中CallBackURI是在登記的回調地址。程序采用MVVM模式,將OAuth2認證的頁面為AuthenticationPage.xaml,登陸的邏輯都在ViewModel對應於AuthenticationViewModel,和QQ互聯服務器交互的是AuthenticationProcess。交互的流程參照【QQ登錄】開發攻略_Client-side

AuthenticationViewModel::GetAccessCode封裝了OAuth2的驗證授權的邏輯。

       private bool _isAuthenticating;
       private Queue<Action<string,string>> _queuedRequests = new Queue<Action<string,string>>();

       public void GetAccessCode(Action<string,string> callback)
       {
           lock (_sync)
           {
               if (_isAuthenticating)
               {
                   _queuedRequests.Enqueue(callback);
               }
               else if (HasAuthenticated)
               {
                   if (!_process.AuthResult.IsExpired)
                   {
                       callback(_process.AuthResult.AccessToken,_process.AuthResult.OpenId);
                   }
                   else
                   {
                       InvokeCallback(callback);
                   }
               }
               else
               {
                   InvokeCallback(callback);
               }
           }
       }

       private void InvokeCallback(Action<string, string> callback)
       {
           _isAuthenticating = true;
           _queuedRequests.Enqueue(callback);

           ((PhoneApplicationFrame)App.Current.RootVisual).Navigate(new Uri("/AuthenticationPage.xaml", UriKind.Relative));
           AuthUri = _process.AuthUri;
       }

1、如果正在認證過程中,把調用方法放到隊列里,然后返回。

2、如果驗證過了,並且票據還是有效的,直接回調方法。

3、如果沒有認證過,或者票據已經失效了,轉到驗證頁面,可以使用QQ號碼登陸。

AuthenticationPage.xaml頁面帶了一個WebBrowser對象,將一個綁定是AuthenticationViewModel的AuthUri ,類似於

http://openapi.qzone.qq.com/oauth/show?which=Login&display=mobile&response_type=token&client_id=204134&redirect_uri=win8charm.com&scope=get_user_info,add_share,list_album,upload_pic,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,add_one_blog,add_topic,get_tenpay_addr&display=mobile

qoauth2windowsphone

用戶登陸后,如果是首次登陸還需要授權API的訪問,然后會返回到redirect_uri參數指定的地址,這里可以拿到返回的用戶的Access Token:

private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
           if (e.Uri.Host.Equals("win8charm.com"))
           {
               webBrowser1.Visibility = Visibility.Collapsed;
               e.Cancel = true;
               // setting this text will bind it back to the view model
               codeBlock.Text = e.Uri.Fragment.Replace("#", "");
           }
}

把返回的AccessToken通過頁面的一個CodeBlock的掩藏TextBlock將結果傳遞給View Model ,將Access Token和OpenID結果解析完成,完成整個驗證過程。

private string _code;
       public string Code
       {
           get
           {
               return _code;
           }
           set
           {
               _code = value;
               _process.ExchangeCodeForToken(Code);
           }
       }

public void ExchangeCodeForToken(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                OnAuthenticationFailed(EventArgs.Empty);
            }
            else
            {
                OAuthToken response = this.restApi.GetUserAccessToken(code);
                GetAccessToken(response);
            }
        }

        void GetAccessToken(OAuthToken response)
        {
            Debug.Assert(response != null);
            AuthResult = new Model.AuthResult()
            { 
                AccessToken = response.AccessToken,
                Expires = response.ExpiresAt
            } ;
            restApi.GetUserOpenIdAsync(AuthResult.AccessToken, GetUserOpenId, GetUserOpenIdFailure);
        }

        void GetUserOpenId(string response)
        {
            if (string.IsNullOrEmpty(response))
            {
                OnAuthenticationFailed(EventArgs.Empty);
            }
            AuthResult.OpenId = response;
            OnAuthenticated();
        }

在認證成功或者失敗的時候引發認證成功或者失敗的事件最終完成整個登陸過程。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM