oauth:example

第三方应用实现企业账户中心开通回调的参考

伪代码范例

code = 获取HTTP GET参数,参数名为code
result = 发送请求给企业QQ开放平台服务器,调用/oauth2/token接口,带上code参数
info = 对 result 进行JSON解码

PHP代码参考范例(未经测试,不可以用于生产环境,主要理解里面的逻辑)

open_callback.php
<?php          
define('OAUTH2_TOKEN', 'https://openapi.b.qq.com/oauth2/token');
define('OPEN_CALLBACKURL', 'http://www.example.com/open_callback'); //此URL需要登记到企业QQ
if (isset($_GET['code'])) {
    $code = $_GET['code'];
    $query = array(
        'grant_type' => 'authorization_code',
        'app_id' => 1300000100,
        'app_secret' => 'u3i465uidsuge',
        'code' => $code,
        'state' => md5(rand()),
        'redirect_uri' => OPEN_CALLBACKURL,
    );
 
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT => 10,
    );
 
    $url = OAUTH2_TOKEN . '?' . http_build_query($query);
    $curl = curl_init($url);
    if (curl_setopt_array($curl, $options)) {
        $result = curl_exec($curl);
    }
    curl_close($curl);
 
 
    if (false !== $result) {
        $company_info = json_decode($result);
        $company_id = $company_info['open_id'];
        $access_token = $company_info['access_token'];
        $time = time();
        $expires_in = $company_info['expires_in'];
        $refresh_token = $company_info['refresh_token'];
        //@todo: 将$company_id, $access_token, $time, $refresh_token, $expires_in存储下来
 
        echo json_encode(array('ret' => 0)); //开放平台读到这个信息,就会开通成功
        die();
    }
}
 
echo json_encode(array('ret' => -1)); //读到这个信息,或者没有读到信息,会开通失败
die();
oauth/example.txt · 最后更改: 2021/05/06 20:17 (外部编辑)