gpt4 book ai didi

oauth-2.0 - Google Oauth2 API - 无刷新 token

转载 作者:行者123 更新时间:2023-12-05 04:09:57 26 4
gpt4 key购买 nike

我尝试了 Stack Exchange 上提供的许多解决方案来获取刷新 token ,但都失败了。下面是我的整个 Controller 。如您所见,我包含了 $client->setAccessType('offline') 和 $client->setApprovalPrompt('force')。

此外,我多次运行撤销 token ,并在此处进入我的 Google 帐户 (https://myaccount.google.com/permissions),并删除了访问权限。当我进行新授权时,这些尝试都没有为我提供刷新 token 。

<?php
/**
* @file
* Contains \Drupal\hello\GAOauthController.
*/

namespace Drupal\ga_reports_per_user\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Google_Client;
use Google_Service_Analytics;
use Drupal\group\Context\GroupRouteContextTrait;

class GAOauthController extends ControllerBase {

use GroupRouteContextTrait;

public function authorize($group = NULL) {
$client = new Google_Client();
$credentials_file = \Drupal::service('file_system')->realpath("private://") . '/client_credentials.json';
$client->setAuthConfig($credentials_file);
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$protocol = "https";
}
else {
$protocol = "http";
}
$redirect_uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . '/finish_google_oauth';
$client->setState($group);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$auth_url = $client->createAuthUrl();
return new TrustedRedirectResponse($auth_url);
}

public function finishOauth() {
if (isset($_GET['code'])) {
$client = new Google_Client();
$credentials_file = \Drupal::service('file_system')->realpath("private://") . '/client_credentials.json';
$client->setAuthConfig($credentials_file);
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$protocol = "https";
}
else {
$protocol = "http";
}
$redirect_uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . '/finish_google_oauth';
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->authenticate($_GET['code']);
$token = $client->getAccessToken();
$refreshToken = $client->getRefreshToken();
$client->setAccessToken($token);

$group_entity = \Drupal::entityTypeManager()->getStorage('group')->load($_GET['state']);

$group_entity->field_google_oauth_token->value = $token['access_token'];
$group_entity->field_google_oauth_token_type->value = $token['token_type'];
$group_entity->field_google_oauth_token_created->value = $token['created'];
$group_entity->field_google_oauth_token_expire->value = $token['expires_in'];

$save_status = $group_entity->save();

return new RedirectResponse('/group/' . $_GET['state']);
}
}

public function revoke($group = NULL){
$client = new Google_Client();
$group_entity = \Drupal::entityTypeManager()->getStorage('group')->load($group);
$result = $client->revokeToken($group_entity->field_google_oauth_token->value);

$group_entity->field_google_oauth_token->value = '';
$group_entity->field_google_oauth_token_type->value = '';
$group_entity->field_google_oauth_token_created->value = '';
$group_entity->field_google_oauth_token_expire->value = '';

$group_entity->save();

return new RedirectResponse('/group/' . $group);

}
}

最佳答案

如果您已经为该登录/开发请求了访问 token 。 token 组合,它不会再为你返回它。您必须前往 https://myaccount.google.com/permissions 撤销访问权限。 .找到您的应用,撤销访问权限,然后重试。

资料来源:我遇到了完全相同的问题,在差点把头发拔出来之后,我想通了。

关于oauth-2.0 - Google Oauth2 API - 无刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45152497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com