QQ微博授权+LIBCURL+MFC(可视化窗口)

来源:互联网 发布:python class 自己 编辑:程序博客网 时间:2024/06/05 20:39

准备工作:

1、创建一个win32可视化程序(直接可以用mfc创建一个对话框并把相关按钮绑定如下的函数即可)

2、micsoft 提供的web控件

3、用一个可以发起http请求的工具类(鄙人根据libcur库封装了一套调用请看代码里面 下载点击:CURL_DOWN)


效果如图:






void CTencentAuthorizationDlg::OnBnClickedButton1(){//登录授权GetDlgItem(IDC_EDIT_URL)->GetWindowText(m_strCallBakURL);GetDlgItem(IDC_EDIT_APP_ID)->GetWindowText(m_strAppID);GetDlgItem(IDC_EDIT_APP_KEY)->GetWindowText(m_strAppKey);GetDlgItem(IDC_EDIT_APP_ID)->GetWindowText(m_strAppID);GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE);CString strUrl;strUrl.Format(_T("https://graph.qq.com/oauth2.0/authorize?""response_type=code&client_id=%s&redirect_uri=%s&state=1&scope=1"),m_strAppID, m_strCallBakURL);m_web.Navigate(strUrl,NULL, NULL, NULL, NULL);GetDlgItem(IDC_EDIT_CODE)->SetWindowText("");GetDlgItem(IDC_EDIT_TOKEN)->SetWindowText("");GetDlgItem(IDC_EDIT_OPENID)->SetWindowText("");GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);}void CTencentAuthorizationDlg::OnBnClickedButton2(){//获取tokenCString strUrl;strUrl.Format(_T("https://graph.qq.com/oauth2.0/token?""grant_type=authorization_code&client_id=%s&""redirect_uri=%s&client_secret=%s&code=%s"),m_strAppID, m_strCallBakURL, m_strAppKey, m_strCode);string strReport;if ( CUrlHttp::Request("get", strUrl.GetBuffer(0), strReport) == CURLE_OK ){CString strTemp = strReport.c_str();strTemp.Replace("access_token=","");int nIndex = strTemp.Find("&expires_in=");if ( nIndex > 0 ){m_strToken = strTemp.Left(nIndex);GetDlgItem(IDC_EDIT_TOKEN)->SetWindowText(m_strToken);GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);}}}void CTencentAuthorizationDlg::OnBnClickedButton3(){//获取openidCString strUrl;strUrl.Format(_T("https://graph.qq.com/oauth2.0/me?access_token=%s"), m_strToken);string strReprot;if ( CUrlHttp::Request("get", strUrl.GetBuffer(0), strReprot) == CURLE_OK ){CString strTemp = strReprot.c_str();int nIndex = strTemp.Find("openid\":\"");if ( nIndex > 0 ){strTemp.Delete(0, nIndex);strTemp.Replace("openid\":\"","");strTemp.Replace("\"} );","");m_strOpenID = strTemp;GetDlgItem(IDC_EDIT_OPENID)->SetWindowText(m_strOpenID);}}}void CTencentAuthorizationDlg::OnBnClickedButton4(){//获取CODECString strURl = m_web.get_LocationURL();if ( strURl.Find("/?code=") > 0 && strURl.Find("&state=1") > 0 ){CString strCode = strURl;strCode.Replace(m_strCallBakURL,"");strCode.Replace("/?code=","");strCode.Replace("&state=1","");GetDlgItem(IDC_EDIT_CODE)->SetWindowText(strCode);m_strCode = strCode;GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);ShellExecute(NULL,"open", "https://connect.qq.com/manage.html#/appauth/user", NULL, NULL, SW_SHOWNORMAL);//m_web.Navigate("https://connect.qq.com/manage.html#/appauth/user", NULL, NULL, NULL, NULL);}}


原创粉丝点击