将 Google App Engine 与 Laravel 和 Socialite 一起使用

Using Google App Engine with Laravel and Socialite

我在通过社交名流 Google oauth 登录时遇到问题。我正在将当前在本地和生产环境中运行的应用程序迁移到 GAE。

我正在使用 Laravel 5.1 的 shpasser gae 包,它运行正常。 第一个登录请求显示 google 权限屏幕,但在回调中我收到 curl 错误。

cURL error 7: (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
in CurlFactory.php line 168
at CurlFactory::createRejection(object(EasyHandle), array('errno' => '7', 'error' => '', 'url' => 'https://accounts.google.com/o/oauth2/token', 'content_type' => null, 'http_code' => '0', 'header_size' => '0', 'request_size' => '0', 'filetime' => '-1', 'ssl_verify_result' => '0', 'redirect_count' => '0', 'total_time' => '0', 'namelookup_time' => '0.080153', 'connect_time' => '0', 'pretransfer_time' => '0', 'size_upload' => '0', 'size_download' => '0', 'speed_download' => '0', 'speed_upload' => '0', 'download_content_length' => '-1', 'upload_content_length' => '-1', 'starttransfer_time' => '0', 'redirect_time' => '0', 'redirect_url' => '', 'primary_ip' => '', 'certinfo' => array(), 'primary_port' => '0', 'local_ip' => '', 'local_port' => '0')) in CurlFactory.php line 132

我尝试将 Laravel 使用的 Guzzle 包中的证书位置更改为此

 final public function setSslVerification($certificateAuthority = true, $verifyPeer = true, $verifyHost = 2)
    {
        $opts = $this->config[self::CURL_OPTIONS] ?: array();

        if ($certificateAuthority === true) {
            // use bundled CA bundle, set secure defaults
            $opts[CURLOPT_CAINFO] = __DIR__ . '/etc/ca-certificates.crt';
            $opts[CURLOPT_SSL_VERIFYPEER] = true;
            $opts[CURLOPT_SSL_VERIFYHOST] = 2;
        } elseif ($certificateAuthority === false) {
            unset($opts[CURLOPT_CAINFO]);
            $opts[CURLOPT_SSL_VERIFYPEER] = false;
            $opts[CURLOPT_SSL_VERIFYHOST] = 0;
        } elseif ($verifyPeer !== true && $verifyPeer !== false && $verifyPeer !== 1 && $verifyPeer !== 0) {
            throw new InvalidArgumentException('verifyPeer must be 1, 0 or boolean');
        } elseif ($verifyHost !== 0 && $verifyHost !== 1 && $verifyHost !== 2) {
            throw new InvalidArgumentException('verifyHost must be 0, 1 or 2');
        } else {
            $opts[CURLOPT_SSL_VERIFYPEER] = $verifyPeer;
            $opts[CURLOPT_SSL_VERIFYHOST] = $verifyHost;
            if (is_file($certificateAuthority)) {
                unset($opts[CURLOPT_CAPATH]);
                $opts[CURLOPT_CAINFO] = $certificateAuthority;
            } elseif (is_dir($certificateAuthority)) {
                unset($opts[CURLOPT_CAINFO]);
                $opts[CURLOPT_CAPATH] = $certificateAuthority;
            } else {
                throw new RuntimeException(
                    'Invalid option passed to ' . self::SSL_CERT_AUTHORITY . ': ' . $certificateAuthority
                );
            }
        }

        $this->config->set(self::CURL_OPTIONS, $opts);

        return $this;
    }

但还是出现同样的错误。我的 php.ini 文件中也有这个

; enable function that are disabled by default in the App Engine PHP runtime
google_app_engine.enable_functions = "php_sapi_name, php_uname, getmypid"
google_app_engine.allow_include_gs_buckets = "my-bucket-name"
allow_url_include = 1
extension = "curl.so"
google_app_engine.enable_curl_lite = “1”

运行 没有选择,除了可能在没有 Socialite 的情况下重做登录并仅使用 Guzzle,看看我是否仍然遇到错误。

更新 在文档中,它指出您不能同时激活 curl lite 和 full curl。这已被更改,因此 php.ini 文件中只有 extension = "curl.so"。它没有解决问题,但需要更改

经过大量谷歌搜索和研究,我设法解决了这个问题,也感谢这个问题的用户 Omega

在Laravel Socialite 中使用Guzzle 包,此包安装在Google App Engine 上时无法找到证书文件。最近的提交添加了行

// Google app engine
+        '/etc/ca-certificates.crt',

default_ca_bundle function

如果您还没有更新到最新的 Guzzle,请添加此行。目录是

vendor/guzzlehttp/guzzle/src/functions.php

确保您没有在 php.ini 文件中启用扩展名 curl.so。