xaml代码:
<Window x:Class="WpfApp1.QQFormCef" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" xmlns:cef="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" mc:Ignorable="d" Title="QQFormCef" Height="450" Width="800"> <Grid> <cef:ChromiumWebBrowser x:Name="browser" ></cef:ChromiumWebBrowser> </Grid> </Window>
后台:
namespace WpfApp1 { /// <summary> /// QQFormCef.xaml 的交互逻辑 /// </summary> public partial class QQFormCef : Window { public QQFormCef() { InitializeComponent(); // 使用cefsharp 如果使用webbrowser,会出现win7环境中拿不全跳转的地址 browser.Address = "xxx"; browser.FrameLoadStart += Browser_FrameLoadStart; } /// <summary> /// 应用的APPID /// </summary> private string app_id = "xxx"; /// <summary> /// 应用的APPKEY /// </summary> private string app_secret = "xxx"; /// <summary> /// 成功授权后的回调地址 /// </summary> private string myredirect_uri = "xxx"; /// <summary> /// 1:成功 0:失败 /// </summary> public int IsSuccessed { get; set; } private void Browser_FrameLoadStart(object sender, CefSharp.FrameLoadStartEventArgs e) { // string onlineId = LoginUserGlobal.OnlineId; string url = e.Url; if (url.Contains("access_token")) { string accessToken = Get_AccessToken(url); Dispatcher.Invoke(new Action(() => { UserLoin(accessToken); })); return; } } //Step2:一般情况是通过Authorization Code获取Access Token,这里直接从网址获取Access Token protected string Get_AccessToken(string strTempToken) { if (string.IsNullOrEmpty(strTempToken)) return null; //qq最终获得的Access_token string AccessToken = ""; int iStart = strTempToken.IndexOf("="); int iEnd = strTempToken.LastIndexOf('&'); if (iStart < iEnd) { int codeLength = iEnd - iStart - 1; AccessToken = strTempToken.Substring(iStart + 1, codeLength); } return AccessToken; } //Step3:使用Access Token来获取用户的OpenID protected string Get_OpenID(string Access_token) { string callback = JSONHelper.GetJson("https://graph.qq.com/oauth2.0/me?access_token=" + Access_token); int ibegin = callback.IndexOf("{"); int iEnd = callback.IndexOf("}"); int ilength = 0; if (ibegin < iEnd) { ilength = iEnd - ibegin; } string strOpenId = callback.Substring(ibegin, ilength + 1); QQCallBack qqOpenId = JSONHelper.ParseFromJson<QQCallBack>(strOpenId); string openId = qqOpenId.openid; return openId; } //Step4: 调用OpenAPI获取各种信息 protected QQUser Get_qqUserInfo(string Access_token, string openId) { string userInfostr = JSONHelper.GetJson("https://graph.qq.com/user/get_user_info?access_token=" + Access_token + "&oauth_consumer_key=" + app_id + "&openid=" + openId); QQUser qqUser = JSONHelper.ParseFromJson<QQUser>(userInfostr); return qqUser; } #region 用户登录 private void UserLoin(string access_token) { if (string.IsNullOrEmpty(access_token)) return; string openid = Get_OpenID(access_token); if (string.IsNullOrEmpty(openid)) { return; } QQUser qqUser = Get_qqUserInfo(access_token, openid); Close(); } #endregion } }
标签:qq,access,return,string,int,Access,token,第三方,客户端 From: https://www.cnblogs.com/anjingdian/p/17353504.html