【问题标题】:oauth2-client for Google working in Codeigniter在 Codeigniter 中工作的 Google 的 oauth2-client
【发布时间】:2014-05-16 14:48:03
【问题描述】:

您好,有点问题,我找不到任何线索,就这样

我正在使用来自 here 的 OAuth2-client,并且正在使用 codeigniter

这是我的控制器:

public function index()
    {
        include(APPPATH.'libraries/League/OAuth2/Client/Provider/IdentityProvider.php');
        include(APPPATH.'libraries/League/OAuth2/Client/Provider/Google.php');
        $provider = new League\OAuth2\Client\Provider\Google(array(
        'clientId'  =>  '********************.apps.googleusercontent.com',
        'clientSecret'  =>  '**************************',
        'redirectUri'   =>  '**************************/oauth2callback'
    ));

    if ( ! isset($_GET['code'])) {

        // If we don't have an authorization code then get one
        $provider->authorize();

    } else {

        try {

            // Try to get an access token (using the authorization code grant)
            $t = $provider->getAccessToken('authorization_code', array('code' => $_GET['code']));

            // NOTE: If you are using Eventbrite you will need to add the grant_type parameter (see below)
            // $t = $provider->getAccessToken('authorization_code', array('code' => $_GET['code'], 'grant_type' => 'authorization_code'));

            try {

                // We got an access token, let's now get the user's details
                $userDetails = $provider->getUserDetails($t);

                foreach ($userDetails as $attribute => $value) {
                    var_dump($attribute, $value) . PHP_EOL . PHP_EOL;
                }

            } catch (Exception $e) {

                // Failed to get user details

            }

        } catch (Exception $e) {

            // Failed to get access token

        }
    }

    }

我获得了 OAuth 类并将其保存到我的库文件夹中

不知何故,我设法打开了谷歌“选择帐户”或“登录”以及的权限请求

但是当我点击接受时,页面会重新加载到这个
url https://mysite.com/oauth2callback?state=****&code=***

最后是 404。

有没有人有更好的解决方案,谁能帮助我。 这是我第一次尝试使用 OAUTH 并在谷歌应用程序引擎中工作,所以请放过我。提前致谢。

【问题讨论】:

  • 您的应用是否配置为响应对mysite.com/oauth2callback的 GET 请求
  • @GwynHowell 不,我只有那个代码,也许这就是我得到 404 的原因,不知道如何回应......
  • 我会回复的
  • @GwynHowell 谢谢,我会试试的。

标签: php codeigniter google-app-engine oauth oauth-2.0


【解决方案1】:

当用户批准 OAuth 时,Google 会使用您向他们注册的 URL 重定向回您的应用,在您的情况下是 /oauth2callback(这实际上是默认设置)。从您发布的代码看来,处理此请求的代码似乎在 index() 函数中。

因此,要修复您的代码,您可以将逻辑从索引函数移动到响应 /oauth2callback 的新处理程序/控制器,或者在您的代码和谷歌云控制台中更改 redirectUri 以指向任何 URL 调用 index() 函数。

【讨论】:

【解决方案2】:

404 表示您没有“oauth2callback”控制器或其中没有索引方法

callback url 是您可以获取向 oauth 提供者请求的信息的 url

我看到你的网址没有 index.php。如果您的项目的正常 url 有它,那么您输入了错误的 url。解决方案是您需要使用 site_url 函数将绝对回调 url 放入回调参数中

【讨论】:

  • 我尝试创建 oauth2callback 控制器。
  • 是的,创建它并在其中创建方法index(),您可以处理提供者返回的值
  • 不知何故我设法使它工作,但我得到另一个错误 line89 的 identityprovider.php Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Unknown grant "League\OAuth2\Client\Grant\Authorizationcode"'
  • @kodewrecker 你解决了你的问题吗?我遇到了同样的错误!
  • @kodewrecker 我找到了问题,是这个错误吗:github.com/thephpleague/oauth2-client/issues/79
【解决方案3】:

库有bug,看这个:https://github.com/thephpleague/oauth2-client/issues/90

无论如何,我也有同样的问题:不知道如何编写回调控制器。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 2018-05-08
  • 2021-07-14
  • 1970-01-01
  • 2019-01-10
相关资源
最近更新 更多