Laravel 4中的Laravel-Oauth2问题 [英] Laravel-Oauth2 Issue in Laravel 4

查看:95
本文介绍了Laravel 4中的Laravel-Oauth2问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 4的laravel-oauth2软件包来设置Facebook登录.我正在使用此处显示的软件包: https://github.com/madewithlove/laravel-oauth2

I am using the laravel-oauth2 package for Laravel 4 to set up a Facebook login. I am using the package seen here: https://github.com/madewithlove/laravel-oauth2

运行控制登录的页面,public/oauth/facebook,Facebook提示很好,但是当我单击"accept"时,页面吐出以下错误:

When running the page that controls the login, the public/oauth/facebook, the Facebook prompt comes up fine, but when I click "accept", the page spits out the following error:

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()函数如下所示:

The get_user_info() function in Facebook.php looks like this:

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文件如下所示:

My Oauth2Controller.php file looks like this:

<?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);
        }
    }

    }


}

我不确定为什么会出现此错误,因为我遵循了文档和教程.感谢您为解决此问题所提供的帮助.

I am not sure why it is given me this error, as I followed the documentation and tutorials. Thank you for your help with solving this issue.

推荐答案

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

I studied the source code to the package and it appears to be an error in the Facebook Provider (actually most of the providers)

例如,在Facebook提供程序的开头,它执行以下操作:

In the beginning of the Facebook provider for instance it does this:

<?php

use OAuth2\Token\Token_Access;

OAuth2 \ Token命名空间中实际上没有任何类.但是,OAuth2名称空间中只有一个.因此,将上面的值替换为

There is actually no class in the OAuth2\Token namespace. There is however one in the OAuth2 namespace. So replacing the value above with

<?php

use OAuth2\Token_Access;

应解决您的错误.您应该为所有提供商执行此操作.

Should fix your error. You should do this for all the Providers.

PSR命名间隔标准指示下划线映射到目录.因此通常会在./OAuth/Token/Access.php文件中查找OAuth\Token_Access

PSR namespacing standards dictates underscores map to directories. So OAuth\Token_Access class will generally be looked for in the ./OAuth/Token/Access.php file

这篇关于Laravel 4中的Laravel-Oauth2问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆