尝试实施Google SignIn时出错

时间:2016-07-26 14:09:46

标签: php google-signin

我正在尝试将Google SignIn添加到my website,但我看不到任何问题,但如果我看到de Page源代码,我会看到此错误:

  

致命错误:在 /home/u289995868/public_html/es/php/glogin/app/classes/GoogleAuth.php中的非对象上调用成员函数createAuthUrl() 26


此档案的代码:

<?php 
class GoogleAuth
{
    protected $client;

    public function __construct(Google_Client $googleClient = null)
    {
        $this->client = $googleClient;

        if($this->client)
        {
            $this->client->setClientId('xxxxxxxx');
            $this->client->setClientSecret('xxxxxxxx');
            $this->client->setRedirectUri('http://barreeeiroo.ga/es/php/glogin/index.php');
            $this->client->setScopes('email');
        }
    }

    public function isLoggedIn()
    {
        return isset($_SESSION['access_token']);
    }

    public function getAuthUrl()
    {
        return $this->client->createAuthUrl();
    }
}
?>


Index.php代码:

<?php
require_once 'app/init.php';
$googleClient = new Google_Client();
$auth = new GoogleAuth();
?>

... Here there is some code for navbar and other, not important

<h1>Prueba de Login de Google</h1>
<?php if(!$auth->isLoggedIn()): ?>
    <a href="<?php echo $auth->getAuthUrl(); ?>">Iniciar Sesión</a>
<?php else: ?>
    Ya has iniciado sesión            
<?php endif; ?>


如果您想查看错误,请转到this website并查看源代码

1 个答案:

答案 0 :(得分:0)

您必须从

更改
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Main extends JLayeredPane {
    static JFrame frame;
    static Main main;
    static Dimension screenSize;
    public Main() {     
        JPanel backPanel = new BackPanel();
        JPanel frontPanel = new FrontPanel();
        add(backPanel, new Integer(7));
        add(frontPanel, new Integer(8));

        new Thread(() -> {
            while (true){
                repaint();
            }
        }).start();

        RepaintManager.currentManager(backPanel).markCompletelyClean(backPanel);

    }

    public static void main(String[] args) {
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        frame = new JFrame("Game"); // Just use the constructor

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        main = new Main();
        frame.add(main, BorderLayout.CENTER);

        frame.pack();
        frame.setSize(screenSize);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);



    }
    public class BackPanel extends JPanel{
        public boolean drawn = false;
        public BackPanel(){
            setVisible(true);
            setOpaque(false);
            setSize(screenSize);
            JLabel test1 = new JLabel("Test1");
            JLabel test2 = new JLabel("Test2");
            add(test1);
            add(test2);
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillRect(0, 0, screenSize.width, 200);
            System.out.print("Reccursion");
        }
    }
    public class FrontPanel extends JPanel{

        public FrontPanel(){
            setVisible(true);
            setOpaque(false);
            setSize(screenSize);
            JLabel test = new JLabel("Test");
            add(test);
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.fillRect(0+screenSize.width/2, 0, screenSize.width/4, 300);
        }
    }
}

$auth = new GoogleAuth();

如果您检查$auth = new GoogleAuth($googleClient); 的构造函数:

GoogleAuth

您会看到默认为public function __construct(Google_Client $googleClient = null) 。这意味着如果不向构造函数传递任何内容,则null参数将为$googleClient

快速RDD

  • {require and instantiate}
  • 创建一个null的实例,不传递任何参数,并将对象放入GoogleAuth;
    • 由于参数默认为$authnull$googleClient;
    • null设为GoogleAuth::$client的值(所以,$googleClient);
  • {check stuff}
  • 致电null;
    • 致电GoogleAuth::getAuthUrl()(由于GoogleAuth::$client->createAuthUrl();GoogleAuth::$client,所以它几乎就像您在呼叫null;

最后一步使人们更容易理解错误。