majiameng / OAuth2

通用第三方登录,支持Wechat,微信,QQ,Sina,支付宝,Facebook,Line,Twitter,Google ,Pc/Mobile/App login
https://github.com/majiameng/OAuth2/wiki
MIT License
63 stars 11 forks source link

hotfix: 修复Google getUserInfo获取异常 #5

Closed Huang-junsen closed 4 months ago

Huang-junsen commented 11 months ago

原 Google getUserInfo异常 $headers = ['Authorization : Bearer ' . $this->token['access_token']]; $data = $this->get(self::API_BASE . 'oauth2/v2/userinfo', '', $headers);

image

修复后

public function getUserInfo() { if($this->type == 'app'){//App登录 if(!isset($_REQUEST['code']) ){ throw new \Exception("Google APP登录 需要传输code参数! "); } } $this->getToken();

    $headers = array(
        'Authorization: Bearer ' . $this->token['access_token'],
    );

    // 创建请求上下文
    $context = stream_context_create(array(
        'http' => array(
            'header' => $headers,
        ),
    ));

    try {
        $data = file_get_contents(self::API_BASE . 'oauth2/v2/userinfo', false, $context);
        // 检查响应是否成功
        if ($data !== false) {
            return json_decode($data, true);
        } else {
            throw new \Exception("请求失败 " . $data);
        }
    }catch (Exception $exception){
        throw new \Exception("异常 " . $exception);
    }
}
majiameng commented 4 months ago

1.使用的其他get请求组件,组件升级后会解决这个问题,无需修改当前代码 2.在国内服务器由于请求google接口会出现此问题,使用国外、hk服务器正常