1

J'ai essayé de nombreuses solutions fournies sur Stack Exchange pour obtenir un jeton d'actualisation, et toutes échouent. Voici mon contrôleur entier. Comme vous pouvez le voir, j'ai inclus $ client-> setAccessType ('offline') et $ client-> setApprovalPrompt ('force').Google Oauth2 API - No Refresh Token

En outre, j'ai lancé le jeton de révocation plusieurs fois, et je suis entré dans mon compte Google ici (https://myaccount.google.com/permissions), et l'accès a été supprimé. Aucune de ces tentatives ne m'a fourni un jeton d'actualisation lorsque j'ai effectué une nouvelle autorisation.

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

 
    } 
 
}

Répondre

0

Si vous avez déjà demandé un jeton d'accès pour cette connexion/dev. Combo de jetons, il ne le retournera pas pour vous. Vous devrez révoquer l'accès en vous rendant au https://myaccount.google.com/permissions. Recherchez votre application, révoquez l'accès et réessayez. Source: J'ai eu exactement le même problème et après avoir presque tiré mes cheveux, j'ai compris.

+0

Voir mon message original ... "En outre, j'ai lancé le jeton de révocation plusieurs fois, et je suis allé dans mon compte Google ici (https://myaccount.google.com/permissions), et l'accès supprimé Aucune de ces tentatives ne m'a fourni un jeton de rafraîchissement lorsque j'ai fait une nouvelle autorisation. " –

+0

Ah, raté ça. Bummer :( –