简体   繁体   English

致命错误 - 未找到类'League \\ OAuth2 \\ Client \\ Provider \\ AbstractProvider'(phpMailer)

[英]Fatal Error - Class 'League\OAuth2\Client\Provider\AbstractProvider' not found (phpMailer)

I installed league/oauth2-client with composer and it created this line in composer.json 我用composer安装了league/oauth2-client ,它在composer.json创建了这一行

"league/oauth2-client": "2.2.0" “league / oauth2-client”:“2.2.0” 在此输入图像描述

When I refreshed get_oauth_token.php page, this error still came out: 当我刷新get_oauth_token.php页面时,这个错误仍然出现了:

Fatal error: Class 'League\\OAuth2\\Client\\Provider\\AbstractProvider' not found in C:\\xampp\\htdocs...\\PHPMailer\\get_oauth_token.php on line 35 致命错误:第35行的C:\\ xampp \\ htdocs ... \\ PHPMailer \\ get_oauth_token.php中找不到类'League \\ OAuth2 \\ Client \\ Provider \\ AbstractProvider'

Here's get_oauth_token.php 这是get_oauth_token.php

<?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;

require './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 = isset($_SERVER['HTTPS']) ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
//$redirectUri = 'http://localhost/phpmailer/get_oauth_token.php';

//These details obtained are by setting up app in Google developer console.
$clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
$clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';

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(
        'myClientId' => $clientId, //already inserted
        'myClientSecret' => $clientSecret, //already inserted
        'myRedirectUri' => $redirectUri, //already inserted
        '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();
}

Can you explain me in detail what to do after having installed league/oauth2-client through composer require league/oauth2-client ? 你可以通过composer require league/oauth2-client安装league/oauth2-client之后详细解释一下我该做什么吗? Thank you. 谢谢。

Had a similar issue and discovered its not stated explicitly in the official tutorial here . 有一个类似的问题,发现它没有在这里的官方教程中明确说明。 The library league/oauth2-client requires you to install the provider you need seperately as there are multiple providers both official and third party here . 图书馆联盟/ OAuth2用户的客户端需要你,因为有多个供应商的官方和第三方安装你所需要的供应商seperately 这里

For google provider you would need 对于Google提供商,您需要

composer require league/oauth2-google

And refer to it like this 并且像这样参考它

use League\OAuth2\Client\Provider\Google;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM