簡體   English   中英

oauth2-client for Google在Codeigniter工作

[英]oauth2-client for Google working in Codeigniter

你有點問題,我找不到任何領先,這里就是

我正在從這里使用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並在谷歌應用程序引擎中工作所以請饒我。 提前致謝。

當用戶批准OAuth時,Google會使用您在其中注冊的網址重定向回您的應用,在您的情況下,這是/ oauth2callback(這實際上是默認值)。 從您發布的代碼看,處理此請求的代碼在index()函數中。

因此,要解決你的代碼,你可以從索引功能移動邏輯到一個新的處理器/控制器,這是為了響應/ oauth2callback,或改變redirectUri,無論是在你的代碼,並在谷歌雲控制台重新指向任何URL電話index()函數。

404意味着你沒有“oauth2callback”控制器或者沒有索引方法

回調網址是網址,您可以向oauth提供商索取信息

我看到你的網址沒有index.php。 如果你的項目的普通網址有它,你就會輸入錯誤的網址。 解決方案是你需要使用site_url函數將絕對回調url放在回調參數中

庫中有一個錯誤,請看: https//github.com/thephpleague/oauth2-client/issues/90

無論如何,我有同樣的問題:不知道如何編碼回調控制器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM