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

Google登录 getUserInfo时 #4

Closed Huang-junsen closed 4 months ago

Huang-junsen commented 11 months ago

image public function getUserInfo() { $this->getToken();

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

改为 下面的

$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 9 months ago

您需要拥有一个海外的服务哦,否则不可以调用google的登录接口,所以导致访问超时