QQ互联OAuth
/**
* QQ互联 oauth
* @author dyllen
*
*/
class Oauth
{
//取Authorization Code Url
const PC_CODE_URL = 'https://graph.qq.com/oauth2.0/authorize'; //取Access Token Url
const PC_ACCESS_TOKEN_URL = 'https://graph.qq.com/oauth2.0/token'; //取用户 Open Id Url
const OPEN_ID_URL = 'https://graph.qq.com/oauth2.0/me'; //用户授权之后的回调地址
public $redirectUri = null; // App Id
public $appid = null; //App Key
public $appKey = null; //授权列表
//字符串,多个用逗号隔开
public $scope = null; //授权code
public $code = null; //续期access token的凭证
public $refreshToken = null; //access token
public $accessToken = null; //access token 有效期,单位秒
public $expiresIn = null; //state
public $state = null; public $openid = null; //construct
public function __construct($config=[])
{
foreach($config as $key => $value) {
$this->$key = $value;
}
} /**
* 得到获取Code的url
* @throws \InvalidArgumentException
* @return string
*/
public function codeUrl()
{
if (!$this->redirectUri) {
throw new \Exception('parameter $redirectUri must be set.');
}
$query = [
'response_type' => 'code',
'client_id' => $this->appid,
'redirect_uri' => $this->redirectUri,
'state' => $this->getState(),
'scope' => $this->scope,
]; return self::PC_CODE_URL . '?' . http_build_query($query);
} /**
* 取access token
* @throws Exception
* @return boolean
*/
public function getAccessToken()
{
$params = [
'grant_type' => 'authorization_code',
'client_id' => $this->appid,
'client_secret' => $this->appKey,
'code' => $this->code,
'redirect_uri' => $this->redirectUri,
]; $url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params);
$content = $this->getUrl($url);
parse_str($content, $res);
if ( !isset($res['access_token']) ) {
$this->thrwoError($content);
} $this->accessToken = $res['access_token'];
$this->expiresIn = $res['expires_in'];
$this->refreshToken = $res['refresh_token']; return true;
} /**
* 刷新access token
* @throws Exception
* @return boolean
*/
public function refreshToken()
{
$params = [
'grant_type' => 'refresh_token',
'client_id' => $this->appid,
'client_secret' => $this->appKey,
'refresh_token' => $this->refreshToken,
]; $url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params);
$content = $this->getUrl($url);
parse_str($content, $res);
if ( !isset($res['access_token']) ) {
$this->thrwoError($content);
} $this->accessToken = $res['access_token'];
$this->expiresIn = $res['expires_in'];
$this->refreshToken = $res['refresh_token']; return true;
} /**
* 取用户open id
* @return string
*/
public function getOpenid()
{
$params = [
'access_token' => $this->accessToken,
]; $url = self::OPEN_ID_URL . '?' . http_build_query($params); $this->openid = $this->parseOpenid( $this->getUrl($url) ); return $this->openid;
} /**
* get方式取url内容
* @param string $url
* @return mixed
*/
public function getUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch); return $response;
} /**
* post方式取url内容
* @param string $url
* @param array $keysArr
* @param number $flag
* @return mixed
*/
public function postUrl($url, $keysArr, $flag = )
{
$ch = curl_init();
if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_exec($ch); curl_close($ch);
return $ret;
} /**
* 取state
* @return string
*/
protected function getState()
{
$this->state = md5(uniqid(rand(), true));
//state暂存在缓存里面
//自己定义
//。。。。。。。。。 return $this->state;
} /**
* 验证state
* @return boolean
*/
protected function verifyState()
{
//。。。。。。。
} /**
* 抛出异常
* @param string $error
* @throws \Exception
*/
protected function thrwoError($error)
{
$subError = substr($error, strpos($error, "{"));
$subError = strstr($subError, "}", true) . "}";
$error = json_decode($subError, true); throw new \Exception($error['error_description'], (int)$error['error']);
} /**
* 从获取openid接口的返回数据中解析出openid
* @param string $str
* @return string
*/
protected function parseOpenid($str)
{
$subStr = substr($str, strpos($str, "{"));
$subStr = strstr($subStr, "}", true) . "}";
$strArr = json_decode($subStr, true);
if(!isset($strArr['openid'])) {
$this->thrwoError($str);
} return $strArr['openid'];
}
}
QQ互联OAuth的更多相关文章
- PHP版QQ互联OAuth示例代码分享
) { $ch = curl_init(); if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); cu ...
- QQ互联登录回调路径错误redirect uri is illegal(100010)
QQ互联登录设置的路径设置
- QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码(转)
OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...
- QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码
OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...
- 一元云购qq互联回调地址错误解决办法
经过追踪,点击登录后调用 system/modules/api/下面的qqlogin.action.class.php 里面又调用了qq 互联php接口样例里的QC.php的QC类的方法qq_logi ...
- 登陆整合实现-QQ互联认证(ASP.NET版本)
原文:登陆整合实现-QQ互联认证(ASP.NET版本) 首先 我们创建一个qq.ashx的页面,这个页面会跳转到QQ的请求界面 代码如下: QQSettingConfig qqSettingConfi ...
- QQ互联登录提示redirect uri is illegal(100010)完美解决方法
大概2015年3月低,腾讯QQ互联开发平台调整了有关QQ登录应用回调地址填写规则,用来修复QQ登录过程因回调地址的漏洞可能导致存在的安全问题. 博主接触这块较多,但也是四月才了解此事,从4月起,所有新 ...
- QQ登录整合/oauth2.0认证-02-跳转到QQ互联页
---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ...
- QQ互联
[移动应用接入概述] QQ互联开放平台为第三方移动应用提供了丰富的API.第三方移动应用接入QQ互联开放平台后,即可通过调用平台提供的API实现用户使用QQ账号登录移动应用功能,且可以获取到腾讯QQ用 ...
随机推荐
- php过滤ascii控制字符
还记得以前在工作中,将爬来的其它网站的数据导到xml.但是会遇到一个问题:即网页会有ascII的控制字符. 一开始以为是别人为了防止采集而加入的,然后发现一个就往过滤表里加一个.直到慢慢发现,他们都是 ...
- aspx、ashx、asmx文件处理请求效率比较
人生总是面临着许多抉择许多困惑!作为一名“攻城师”或“程序猿”的我们,工作的时候更是如此.你曾经是否苦恼过在系统中使用哪种文件编写客户端请求最合适或最高效呢?aspx.ashx.asmx到底该如何选择 ...
- React JS快速入门教程
翻译至官方文档<Tutorial>http://facebook.github.io/react/docs/tutorial.html 转载请注明出处:http://blog.csdn.n ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- springMVC 访问404
问题:404 但是其他的controller可以访问!!!
- ACM/ICPC 之 欧拉回路两道(POJ1300-POJ1386)
两道有关欧拉回路的例题 POJ1300-Door Man //判定是否存在从某点到0点的欧拉回路 //Time:0Ms Memory:116K #include<iostream> #in ...
- ALV 输入数据时数字缩小一千倍的问题解决
这个字段后面有三位小数 我输入 1 一回车 就变成了0.001 了 解决方法 DATA: wa_fieldcatalog TYPE lvc_s_fcat, "显示数据列内表工作 ...
- 跨浏览器的事件对象-------EventUtil 中的方法及用法
什么是EventUti----封装好的事件对象 在JavaScript中,DOM0级.DOM2级与旧版本IE(8-)为对象添加事件的方法不同 为了以跨浏览器的方式处理事件,需要编写一段“通用代码”,即 ...
- 【leetcode】Plus One (easy)
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- myEclipse中改了项目名,出现的问题 和 错误java.io.IOException: tmpFile.renameTo(classFile) failed
今天遇到一个很头疼的问题,建的一个新项目,后来因为一些原因把项目名改了,之后就做了一些业务,但运行时总是没有反应,后来在myEclipse工作空间下的webapps文件中发现, 部署的文件名和项目名称 ...