0

我正在使用 Laravel 4 的 laravel-oauth2 包来设置 Facebook 登录。我正在使用这里看到的包: https ://github.com/madewithlove/laravel-oauth2

运行控制登录的页面时,public/oauth/facebook,Facebook 提示出现正常,但是当我单击“接受”时,页面吐出以下错误:

ErrorException
Argument 1 passed to OAuth2\Provider\Facebook::get_user_info() must be an instance of OAuth2\Token\Token_Access, instance of OAuth2\Token_Access given, called in /Applications/MAMP/htdocs/crowdsets/laravel-master/app/controllers/Oauth2Controller.php on line 36 and defined

它指向/­vendor/­taylorotwell/­laravel-oauth2/­src/­OAuth2/­Provider/­Facebook.php: 26

Facebook.php 中的get_user_info()函数如下所示:

public function get_user_info(Token_Access $token)
    {
        $url = 'https://graph.facebook.com/me?'.http_build_query(array(
            'access_token' => $token->access_token,
        ));

        $user = json_decode(file_get_contents($url));

        // Create a response from the request
        return array(
            'uid' => $user->id,
            'nickname' => $user->username,
            'name' => $user->name,
            'email' => $user->email,
            'location' => $user->hometown->name,
            'description' => $user->bio,
            'image' => 'https://graph.facebook.com/me/picture?type=normal&access_token='.$token->access_token,
            'urls' => array(
              'Facebook' => $user->link,
            ),
        );
    }

我的 Oauth2Controller.php 文件如下所示:

<?php 

    use OAuth2\OAuth2;
    use OAuth2\Token_Access;
    use OAuth2\Exception as OAuth2_Exception;

class Oauth2Controller extends BaseController
{



    public function getIndex($provider) {


        $provider = OAuth2::provider($provider, array(
        'id' => '****************',
        'secret' => '********************',
        ));

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

            return $provider->authorize();

        }

        else
    {
        // Howzit?
        try
        {
            $params = $provider->access($_GET['code']);

                $token = new Token_Access(array(
                    'access_token' => $params->access_token
                ));
                $user = $provider->get_user_info($token);

            // Here you should use this information to A) look for a user B) help a new user sign up with existing data.
            // If you store it all in a cookie and redirect to a registration page this is crazy-simple.
            echo "<pre>";
            var_dump($user);
        }

        catch (OAuth2_Exception $e)
        {
            show_error('That didnt work: '.$e);
        }
    }

    }


}

我不知道为什么它给了我这个错误,因为我遵循了文档和教程。感谢您帮助解决此问题。

4

1 回答 1

8

我研究了包的源代码,它似乎是 Facebook Provider 中的错误(实际上是大多数提供程序)

例如,在 Facebook 提供程序的开头,它会这样做:

<?php

use OAuth2\Token\Token_Access;

OAuth2\Token 命名空间中实际上没有类。然而,在 OAuth2 命名空间中有一个。所以将上面的值替换为

<?php

use OAuth2\Token_Access;

应该修复你的错误。您应该为所有提供者执行此操作。

PSR 命名空间标准规定下划线映射到目录。所以OAuth\Token_Access一般会在./OAuth/Token/Access.php文件中查找类

于 2013-06-29T03:01:26.223 回答