Use qqconnect oau2on Windows Phone

Source: Internet
Author: User
Tags openid

Qq Internet oauth2.0. net SDK release and website QQ login sample code this article describes the common ASP.. Net websites are connected by QQ. This article mainly introduces how to use QQ to connect oau2sdk in the windowsphone environment. The program in this article is changed from Google oau2on Windows Phone. The process of QQ interconnection is similar to that of Google's oau2, and QQ interconnection is simpler.

The following three class libraries are used in the Code:

  • Restsharp
  • JSON. net
  • Mvvm light

These libraries can be installed through the nuget package. Note that JSON. Net (4.0.7) is not compatible with the latest restsharp version 102.6.0.0. JSON. Net (4.0.5) must be used.

To run this example, you need to go to the http://connect.qq.com registration to get the appkey and appscrect, fill in the configuration file:

Callbackuri is the registered callback address. The program adopts the mvvm mode to set the page for oau22authentication to authenticationpage. XAML. the login logic is that the viewmodel corresponds to authenticationviewmodel, and the interaction with the QQ Internet server is authenticationprocess. For the interaction process, see [QQ logon] development strategy _ client-side.

Authenticationviewmodel: getaccesscode encapsulates the authorization logic of oau22.

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. If the authentication process is in progress, put the calling method in the queue and return it.

2. If the verification is passed and the ticket is still valid, direct callback.

3. If you have not completed the authentication or the ticket has expired, go to the verification page and use the QQ number to log on.

The authenticationpage. XAML page contains a webbrowser object, which binds an authuri bound to the authenticationviewmodel, similar

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, expiration, upload_pic, expiration, add_t, expiration, del_t, expiration, get_info, get_other_info, get_fanslist, get_idolist, add_idol, del_idol, add_one_blog, add_topic, get_tenpay_addr & DISPLAY = Mobile

After a user logs on to the system, if the user needs to authorize API access for the first login, the user will be returned to the address specified by the redirect_uri parameter. Here, the user's access token can be obtained:

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 ("#","");
}
}

The returned accesstoken is passed to the view model through a codeblock hidden textblock on the page. The access token and openid results are parsed to complete the verification process.

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 ();
}

If the authentication succeeds or fails, the logon process is completed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.