【问题标题】:how to get Refresh token in google oauth 2.0 api with PHP如何使用 PHP 在 google oauth 2.0 api 中获取刷新令牌
【发布时间】:2021-03-20 00:49:15
【问题描述】:

我正在使用生成访问令牌并且它工作成功,因为访问令牌的时间很短,只有 1 小时,我想获取用户的刷新令牌并存储在数据库中,以便我可以获得访问权限任何时候我需要的令牌。

下面是我在两个文件中的代码。

文件 Oauth.php

 <?php 
 require  'vendor/autoload.php';
 // Refer to the PHP quickstart on how to setup the environment:

 $client = new Google_Client();
 $client->setAccessType('offline');
 $client->setAuthConfigFile('client_secret.json');  //file downloaded earlier
 $client->addScope("https://www.googleapis.com/auth/calendar");
 $auth_url = $client->createAuthUrl();
 header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); //redirect user to Google

第二个文件 get_token.php

 <?php 
require  'vendor/autoload.php';
// Refer to the PHP quickstart on how to setup the environment:
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
$client->setAccessToken($access_token);
?>

回复我是这样的https://www.xxxxxxxxxxxxxxxxxxx.com/google_calendar/get_token.php?code=4/0AY0e-g7ZauFJQPlzm1KsNpeuTF8S_5alcpjX8TA9LN0GVJd2cD0gAAiDPU56j2C9sVKIfg&scope=https://www.googleapis.com/auth/calendar

提前致谢

【问题讨论】:

    标签: php oauth-2.0 google-oauth


    【解决方案1】:

    当您获得从 Google 返回的访问令牌时,响应正文中的内容是什么?刷新令牌应该在基于 OAuth 2.0 标准的初始响应中传回,您需要将此令牌存储在安全的地方。当您需要刷新访问令牌时,您需要将存储的刷新令牌与请求一起传递(可能在查询字符串中作为参数),然后 Google 会将新的访问令牌发回给您。通读谷歌文档,因为这应该详细解释! 正如您在我从上面的 Google OAuth 获得的响应中看到的那样,第二张图片是您应该得到的响应,重复您请求访问令牌的过程,看看您是否得到了像我在图片中得到的响应,您会注意到响应正文中的 refresh_token。

    【讨论】:

    • 您能否以图像或 JSON 格式的代码发布响应?我无法按照您粘贴的方式阅读。
    • 这个你可以理解,很简单的英文。请发布完整的回复,谢谢,回复在屏幕截图中的字符串末尾被截断
    【解决方案2】:

    我用以下方法做到了这一点

    <?php 
    require  '../vendor/autoload.php';
    $client = new Google_Client();
    $client->setAuthConfig('client_secret_234rcontent.com.json');
    $client->addScope('https://www.googleapis.com/auth/calendar');
    $client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . 
     '/google_calendar2/oauth2callback.php');
     // offline access will give you both an access and refresh token so that
     // your app can refresh the access token without user interaction.
     $client->setAccessType('offline');
     // Using "consent" ensures that your application always receives a refresh token.
    // If you are not using offline access, you can omit this.
    $client->setPrompt('consent');
    $client->setApprovalPrompt("consent");
    $client->setIncludeGrantedScopes(true);   // incremental auth
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    

    重定向 uri 代码

    <?php
    require  'vendor/autoload.php';
    $client = new Google_Client();
    $client->setAuthConfigFile('client_secret_2344056t4.apps.googleusercontent.com.json');
    $client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . 
    '/google_calendar2/oauth2callback.php');
    $client->addScope('https://www.googleapis.com/auth/calendar');
    $credentials=$client->authenticate($_GET['code']);
    $access_token = $client->getAccessToken();
    // $refresh_token = $credentials['refresh_token'];
    print_r($credentials);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 2017-09-05
      • 2014-07-15
      • 2023-01-23
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      相关资源
      最近更新 更多