Google访问令牌为空



我尝试了代码点火器。

下面我把这个代码放在一个构造中

$this->google = new Google_Client();
$this->google->setClientId(GOOGLEID);
$this->google->setClientSecret(GOOGLESECRET);
$this->google->setDeveloperKey(GOOGLEAPIKEY);
$objOAuthService = new Google_Service_Oauth2($this->google);

然后在一个方法1中,我把下面的

$this->google->setRedirectUri(site_url('auth/google-login?'));
$this->google->addScope("email");
$this->google->addScope("profile");
$data['content_data']['google_login_url'] = $this->google->createAuthUrl();
$this->load->view("test", $data);

然后在方法2中,我放入以下

if (isset($_GET['code'])) {
    $this->google->authenticate($_GET['code']);
    $token = $this->google->getRefreshToken();
    echo $token;
}

你可以看到,我试图打印$代币,但它是空的。我的问题是为什么它是空的?

编辑:

echo $this->google->getAccessToken();

甚至getAccessToken也返回空。

我刚刚修复了它。

我的解决方案是,在结构中我有

$this->google = new Google_Client();
$this->google->setClientId(GOOGLEID);
$this->google->setClientSecret(GOOGLESECRET);
$this->google->setDeveloperKey(GOOGLEAPIKEY);
$this->google->setRedirectUri(site_url('auth/google-login?'));
$this->google->addScope("email");
$this->google->addScope("profile");

在方法1中,我只有这个

$data['content_data']['google_login_url'] = $this->google->createAuthUrl();
$this->load->view("test", $data);

在方法2中,我有这个

if (isset($_GET['code'])) {
    $this->google->authenticate($_GET['code']);
    $token = $this->google->getAccessToken();
    var_dump($token);
}

这里的$token现在已经有值

这是我在Laravel和Codeigniter中使用的工作代码。

// Initial config
$client = new Google_Client();
$client->setApplicationName(APP_NAME);
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setScopes(array(SCOPE_A));
// To retrieve refresh token forcefully
// This will ask for permission everytime.
$client->setApprovalPrompt('force');
$client->setAccessType('offline');

// Return url data part
if (isset($_GET['code'])) {
    // Log the user in
    $client->authenticate($_GET['code']);
    // Get acess token and refresh token
    $getAccesToken = $client->getAccessToken();
    $getRefreshToken = $client->getRefreshToken();
} 
else 
{
    // Return to login
    $googleAuthUrl = $client->createAuthUrl();
    // Redirect user to authentication URI
}

我希望这能有所帮助。

相关内容

  • 没有找到相关文章

最新更新