【问题标题】:string(331) "Legacy People API has not been usedstring(331) "未使用旧版 People API
【发布时间】:2021-04-12 21:18:51
【问题描述】:

当我尝试通过 google api 注册时遇到此错误

string(331) "Legacy People API 之前没有在项目 ******* 中使用过或已禁用。通过访问 https://console.developers.google.com/apis/api/legacypeople.googleapis.com/overview?project=******** 启用它然后重试。如果您最近启用了此 API,请稍等几分钟后该操作就会传播到我们的系统并重试。”

当我去那个网址时,我正在接收

加载失败。 加载 /apis/api/legacypeople.googleapis.com/overview?project=******&dcccrf=1 时出错。请重试。

我在 /vendor/league/oauth2-google/src/Provider 中的 google.php 代码是

<?php

namespace League\OAuth2\Client\Provider;

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

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 array Default fields to be requested from the user profile.
     * @link https://developers.google.com/+/web/api/rest/latest/people
     */
    protected $defaultUserFields = [
        'id',
        'name(familyName,givenName)',
        'displayName',
        'emails/value',
        'image/url',
    ];
    /**
     * @var array Additional fields to be requested from the user profile.
     *            If set, these values will be included with the defaults.
     */
    protected $userFields = [];

    /**
     * Use OpenID Connect endpoints for getting the user info/resource owner
     * @var bool
     */
    protected $useOidcMode = false;

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

    public function getBaseAccessTokenUrl(array $params)
    {
        return 'https://www.googleapis.com/oauth2/v4/token';
    }

    public function getResourceOwnerDetailsUrl(AccessToken $token)
    {
        if ($this->useOidcMode) {
            // OIDC endpoints can be found https://accounts.google.com/.well-known/openid-configuration
            return 'https://www.googleapis.com/oauth2/v3/userinfo';
        }
        // fields that are required based on other configuration options
        $configurationUserFields = [];
        if (isset($this->hostedDomain)) {
            $configurationUserFields[] = 'domain';
        }
        $fields = array_merge($this->defaultUserFields, $this->userFields, $configurationUserFields);
        return 'https://www.googleapis.com/plus/v1/people/me?' . http_build_query([
            'fields' => implode(',', $fields),
            'alt'    => 'json',
        ]);
    }

    protected function getAuthorizationParameters(array $options)
    {
        $params = array_merge(
            parent::getAuthorizationParameters($options),
            array_filter([
                'hd'          => $this->hostedDomain,
                'access_type' => $this->accessType,
                // 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)
    {
        $user = new GoogleUser($response);
        // Validate hosted domain incase the user edited the initial authorization code grant request
        if ($this->hostedDomain === '*') {
            if (empty($user->getHostedDomain())) {
                throw HostedDomainException::notMatchingDomain($this->hostedDomain);
            }
        } elseif (!empty($this->hostedDomain) && $this->hostedDomain !== $user->getHostedDomain()) {
            throw HostedDomainException::notMatchingDomain($this->hostedDomain);
        }

        return $user;
    }
}

如何解决这个问题?

【问题讨论】:

    标签: php google-api google-oauth google-developers-console google-people-api


    【解决方案1】:

    Legacy People API 之前没有在项目 ******* 中使用过,或者它已被禁用。通过访问https://console.developers.google.com/apis/api/legacypeople.googleapis.com/overview?project=******** 启用它

    由于错误消息指出您尚未在项目中启用人员 API,并且您已包含 emailprofile 并试图请求有关用户的配置数据。

    return 'https://www.googleapis.com/plus/v1/people/me?' . http_build_query([
            'fields' => implode(',', $fields),
            'alt'    => 'json',
    

    您需要在我们的项目中启用人员 API,然后才能请求数据。点击链接并按照以下说明进行操作。

    Google developer console点击左边的库。然后搜索您要使用的 API 并单击启用按钮

    等待几分钟,然后再次运行您的代码。然后你就可以向 people api 发出请求了。

    return 'https://www.googleapis.com/plus/v1/people/me?' . http_build_query([
            'fields' => implode(',', $fields),
            'alt'    => 'json',
    

    旧端点:

    我还建议将您的端点更新为新的people.get 端点

    https://people.googleapis.com/v1/people/me
    

    【讨论】:

    • 我试过了,但没有名为 OAuth2 的 api ?
    • 如果您阅读错误消息,它不会说 Oauth2,它清楚地指出 Legacy People API has not been used in project 它说 people api
    • 它已经启用但仍然出现错误:(
    • 那么它是错误的项目。它没有启用,谷歌不会骗你。您还应该考虑更新到新端点,而不是使用旧端点。
    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 2020-04-21
    • 2020-06-26
    • 2019-04-20
    • 2021-05-28
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多