【问题标题】:Not geting token Google Oauth未获得令牌 Google Oauth
【发布时间】:2021-05-20 13:27:07
【问题描述】:

我正在尝试使用 google API 登录,但由于某种原因,$_GET["code"]$this->request->getVar('code') 都是空的,因此我无法获取我的令牌。我试过 $_GET["code"] 而不是 $this->request->getVar('code') 但没有任何效果:

Error Imagesecond image

我正在关注这个视频教程:click here

public function signup_with_gmail() {
   
    require_once APPPATH.'Libraries\vendor\autoload.php';
    $google_client = new \Google_Client();
    //$google_client->setApplicationName("pharma");
    $google_client->setClientId('clientID');
    $google_client->setClientSecret('SECRET');
    $google_client->setRedirectUri('http://localhost/pharma/User/sign_in');
    $google_client->addScope('email');
    $google_client->addScope('profile');
    
    print_r($this->request->getVar('code') === NULL);
    print_r($_GET['code']);
    if ($this->request->getVar('code') != NULL) {
        $token = $google_client->fetchAccessTokenWithAuthCode($this->request->getVar('code'));
        if(!$token['error']){
            $google_client->setAccessToken($token['access_token']);
            $session->set('access_token', $token['access_token']);
            //now get profile data create another object
            $google_service = new \Google_Service_Oauth2($google_client);
            $data = $google_service->userinfo->get();
            print_r($data);
        }
    }
    print_r($data);
}

【问题讨论】:

  • 我检查了 github 堆栈溢出但都与令牌刷新有关,但我的问题是 $_GET['code'] 或 $this->request->getVar('code')应该从谷歌返回一些东西,但它是空的

标签: google-oauth codeigniter-4


【解决方案1】:

$this->request->getVar('code') 中的代码只有在您点击使用$google_client->createAuthUrl(); 创建的 URL 时才会生成

所以,你可以这样做......

在控制器中写入 $data['loginButton'] = $google_client->createAuthUrl();

将此$data['loginButton'] 传递到使用 Google 登录按钮的锚标记中的视图。

现在,当您单击 Sign in with Google 按钮时,它将重定向回相同的 localhost.com/login URL,但这次您将在网址。因此,您可以使用 $this->request->getVar('code') 获取它,然后执行其余过程。

  • 请确保 ClientIdClientSecret 等所有详细信息均正确无误
  • 并更新到最新的"google/apiclient": "^2.10"

【讨论】:

    猜你喜欢
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2021-09-07
    • 2021-11-21
    • 2015-06-27
    相关资源
    最近更新 更多