谷歌Oauth2 API - 无刷新令牌

3

我已经尝试了许多在Stack Exchange上提供的解决方案以获取刷新令牌,但它们都失败了。以下是我的整个控制器。如您所见,我已包含 $client->setAccessType('offline') 和 $client->setApprovalPrompt('force')。

此外,我已经多次撤销了令牌,并进入我的Google账户(https://myaccount.google.com/permissions)并移除了访问权限。这些尝试都没有在我进行新的授权时为我提供刷新令牌。

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

  }
}

1个回答

4
如果您已经请求了该登录/开发令牌组合的访问令牌,它不会再次返回给您。您需要前往https://myaccount.google.com/permissions撤销访问权限,然后再试一次。
来源:我曾经遇到过完全相同的问题,在几乎要把头发都拔光的情况下,我解决了这个问题。

1
请看我的原始帖子...此外,我已经多次运行了撤销令牌,并进入我的Google帐户(https://myaccount.google.com/permissions),并删除了访问权限。当我进行新的授权时,这些尝试都没有为我提供刷新令牌。 - Scott Taylor
啊,错过了。真遗憾 :( - Adam Haining

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接