【问题标题】:Having trouble with this : Fatal error: require(): Failed opening required 'vendor/autoload.php'遇到问题:致命错误:require(): Failed opening required 'vendor/autoload.php'
【发布时间】:2017-05-21 01:19:41
【问题描述】:

我曾尝试使用 oAuth2 来使用谷歌云平台 api,但我的重定向 url(PHPMailer 中的 get_oauth_token.php 是我使用的文件)找不到作曲家的自动加载文件:vendor/autoload.php,即使我有composer 和 guzzle 下载并运行。是否有任何其他文件需要我下载或以其他方式自动加载该文件?

这里是 get_oauth_token.php 以防你没有看到它(这里没有发布客户端 ID)

<?php
/**
 * Get an OAuth2 token from Google.
 * * Install this script on your server so that it's accessible
 * as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
 * e.g.: http://localhost/phpmail/get_oauth_token.php
 * * Ensure dependencies are installed with 'composer install'
 * * Set up an app in your Google developer console
 * * Set the script address as the app's redirect URL
 * If no refresh token is obtained when running this file, revoke access to your app
 * using link: https://accounts.google.com/b/0/IssuedAuthSubTokens and run the script again.
 * This script requires PHP 5.4 or later
 * PHP Version 5.4
 */

namespace League\OAuth2\Client\Provider;
//composer require PHPMailer/PHPMailer;
require 'vendor/autoload.php';

//require_once(__DIR__.'/PHPMailer/PHPMailer-master/vendor/autoload.php');

use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;

session_start();

//If this automatic URL doesn't work, set it yourself manually
$redirectUri ='http://localhost:8080/phpmailer/get_oauth_token.php';
//$redirectUri = 'http://localhost/phpmailer/get_oauth_token.php';

//These details obtained are by setting up app in Google developer console.
$clientId = '';
$clientSecret = '';

class Google extends AbstractProvider
{
    use BearerAuthorizationTrait;

    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id';

    /**
     * @var string If set, this will be sent to google as the "access_type" parameter.
     * @link https://developers.google.com/accounts/docs/OAuth2WebServer#offline
     */
    protected $accessType;

    /**
     * @var string If set, this will be sent to google as the "hd" parameter.
     * @link https://developers.google.com/accounts/docs/OAuth2Login#hd-param
     */
    protected $hostedDomain;

    /**
     * @var string If set, this will be sent to google as the "scope" parameter.
     * @link https://developers.google.com/gmail/api/auth/scopes
     */
    protected $scope;

    public function getBaseAuthorizationUrl()
    {
        return 'https://accounts.google.com/o/oauth2/auth';
    }

    public function getBaseAccessTokenUrl(array $params)
    {
        return 'https://accounts.google.com/o/oauth2/token';
    }

    public function getResourceOwnerDetailsUrl(AccessToken $token)
    {
    return ' ';
    }

    protected function getAuthorizationParameters(array $options)
    {
    if (is_array($this->scope)) {
            $separator = $this->getScopeSeparator();
            $this->scope = implode($separator, $this->scope);
        }

        $params = array_merge(
            parent::getAuthorizationParameters($options),
            array_filter([
                'hd'          => $this->hostedDomain,
                'access_type' => $this->accessType,
        'scope'       => $this->scope,
                // if the user is logged in with more than one account ask which one to use for the login!
                'authuser'    => '-1'
            ])
        );
        return $params;
    }

    protected function getDefaultScopes()
    {
        return [
            'email',
            'openid',
            'profile',
        ];
    }

    protected function getScopeSeparator()
    {
        return ' ';
    }

    protected function checkResponse(ResponseInterface $response, $data)
    {
        if (!empty($data['error'])) {
            $code  = 0;
            $error = $data['error'];

            if (is_array($error)) {
                $code  = $error['code'];
                $error = $error['message'];
            }

            throw new IdentityProviderException($error, $code, $data);
        }
    }

    protected function createResourceOwner(array $response, AccessToken $token)
    {
        return new GoogleUser($response);
    }
}


//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
$provider = new Google(
    array(
        'clientId' => $clientId,
        'clientSecret' => $clientSecret,
        'redirectUri' => $redirectUri,
        'scope' => array('https://mail.google.com/'),
    'accessType' => 'offline'
    )
);

if (!isset($_GET['code'])) {
    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: ' . $authUrl);
    exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
    unset($_SESSION['oauth2state']);
    exit('Invalid state');
} else {
    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken(
        'authorization_code',
        array(
            'code' => $_GET['code']
        )
    );

    // Use this to get a new access token if the old one expires
    echo 'Refresh Token: ' . $token->getRefreshToken();
}

【问题讨论】:

    标签: php api email composer-php google-cloud-platform


    【解决方案1】:

    在这种情况下,您可能会忘记文件路径。 试试require "/path/to/vendor/autoload.php";

    【讨论】:

    • 我什至不确定要放置什么路径,因为我没有将它保存为文件,但由于我安装了 composer 和 guzzle,它应该已经安装了那个包
    • 你使用的是什么操作系统?
    • 我用的是windows
    • 好的,值得一试。尝试找出 composer 和 guzzle 的默认安装目录在哪里。如果您更大胆和耐心,您可以尝试在文件浏览器中搜索 autoload.php。找到后,输入它的路径,它应该可以工作。另外,也许您可​​以尝试手动安装它?
    • 谢谢我找到了文件,现在 url 可以工作了,但是当我尝试获取令牌时,我收到了这个错误:致命错误:未捕获的异常 'GuzzleHttp\Exception\RequestException' 并带有消息'cURL 错误 60:SSL证书问题:无法获取本地颁发者证书我已经下载了最新的 cacert.pem 文件并将这一行添加到我的 php.ini 文件中:curl.cainfo=/path/to/downloaded/cacert.pem 具有正确的路径,什么我应该怎么做?
    猜你喜欢
    • 2021-03-22
    • 2015-01-09
    • 2016-03-09
    • 2023-04-02
    • 1970-01-01
    • 2019-10-25
    • 2020-04-18
    • 2015-04-08
    • 2016-10-20
    相关资源
    最近更新 更多