简体   繁体   中英

oauth2-client for Google working in Codeigniter

Hi got a bit problem and I can't find any lead, here it goes

I'm working with OAuth2-client from here and I'm doing it in codeigniter

this is my controller :

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

        }
    }

    }

I get the OAuth class and save it to my library folder

Somehow I manage to open the google "Choose Account" or "login" and the request for permission this 在此输入图像描述

but when I click Accept the page reload to this
url https://mysite.com/oauth2callback?state=****&code=***

and that is 404 in my end.

does anyone have better solution to this, could anyone help me. this is my 1st try using OAUTH and working in google app engine so please spare me. Thanks in advance.

When the user approves the OAuth, Google redirects back to your app with the URL you registered with them, in your case this is /oauth2callback (this is actually the default). It looks as though, from the code you have posted, that the code to deal with this request is in the index() function.

So, to fix you code, you can either move the logic from the index function to a new handler / controller which responds to /oauth2callback, or change the redirectUri, both in your code and at google cloud console to point back to whatever URL calls the index() function.

404 mean you dont have "oauth2callback" controller or dont have index method in it

callback url is url you can get information requested to oauth provider

I see your url dont have index.php. If normal url of your project have it, you are putting wrong url. Solution is you need to put absolute callback url in callback param by using site_url function

There is a bug in the library, look at this: https://github.com/thephpleague/oauth2-client/issues/90

Anyway, i have the same problem: don't know how to code the callback controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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