我该怎么做刷新令牌(谷歌日历)

时间:2017-05-02 08:27:07

标签: oauth-2.0 google-calendar-api

enter image description here

过去好几天我浪费了很多时间 现在我设法获得刷新令牌..我之前无法获得刷新令牌。

我的目的是在没有Google用户登录的情况下同步Google日历数据

我做了什么:

  1. 我将令牌信息存储在我的数据库中
  2. 当用户想要同步时,我使用存储的信息从Google日历API中提取数据 但是令牌有到期日,即一小时
  3. 我假设我可以使用刷新令牌获取可用的令牌并再次从Google日历api中提取数据(但是如何?)
  4. 额外的问题:我可以从之前的刷新令牌获得另一个刷新令牌吗?是不是我可以一遍又一遍地获得刷新令牌?

    这是我的一些代码:

     public function __construct()
    {
        $client = new Google_Client();
        $client->setAuthConfig('client_secret.json');
        $client->addScope(Google_Service_Calendar::CALENDAR); // CALENDAR READONLY 는 읽기만 가능
        $guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false)));
        $client->setHttpClient($guzzleClient);
        $client->setAccessType ("offline");
        $client->setApprovalPrompt ("force");
        $client->setRedirectUri('http://localhost/smart_mirror/GoogleCalendarApi-master/public/api/cal');
        $this->client = $client;
    }
    
    
    $refresh_token=$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken()); 
    $this->client->getAuth()->refreshToken($this->client->getAuth()->getRefreshToken()); 
    $refresh_token=$this->client->getAccessToken();
    

1 个答案:

答案 0 :(得分:0)

  

访问令牌的生命周期有限。如果您的应用需要访问权限   对于超出单一访问令牌生命周期的Google API,它可以   获取刷新令牌。刷新令牌允许您的应用程序   获得新的访问令牌。

     

注意:在安全的长期存储中保存刷新令牌并继续   只要它们仍然有效,就使用它们。限制适用于数量   刷新每个客户端 - 用户组合和每个发布的令牌   所有客户的用户,这些限制是不同的。如果你的   应用程序请求足够的刷新令牌来覆盖其中一个   限制,旧的刷新令牌停止工作。

Refreshing an access token (offline access)

如果您的应用需要离线访问Google API,请将API客户端的访问类型设置为离线:

import java.util.*;
import java.lang.*;

public class Server{
private static String ligning = y=3x^3-2x^2+4x-6;
private static String replacedLigning;
private static int i = 1;
static ArrayList xVærdi = new ArrayList();
static ArrayList x2Værdi = new ArrayList();
static ArrayList x3Værdi = new ArrayList();

while(erTal(ligning.substring((ligning.indexOf("x^3")-i),
(ligning.indexOf("x^3")-i+1))) == true){

x3Værdi.add(ligning.substring((ligning.indexOf("x^3")-i),
(ligning.indexOf("x^3")-i+1)));
                        i++;
                    }

                    replacedLigning = ligning.replace("x^3","");
                    while(erTal(replacedLigning.substring((replacedLigning.indexOf("x^2")-i),(replacedLigning.indexOf("x^2")-i+1))) == true){
                        x2Værdi.add(replacedLigning.substring((replacedLigning.indexOf("x^2")-i),(replacedLigning.indexOf("x^2")-i+1)));
                        i++;
                    }

                   System.out.println(replacedLigning); //gives: y=3-2x^2+4x-6
                    replacedLigning = replacedLigning.replace("x^2","");

                    System.out.println(replacedLigning); //gives y=3-2+4x-6

                    i=1;
                    while(erTal(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1))) == true){
                        xVærdi.add(replacedLigning.substring((replacedLigning.indexOf("x")-i),(replacedLigning.indexOf("x")-i+1)));
                        i++;
                    }

                    Collections.reverse(xVærdi);
                    Collections.reverse(x2Værdi);
                    Collections.reverse(x3Værdi);

                    System.out.println(xVærdi); **//Contains nothing: [] should contain [4]**
                    System.out.println(x2Værdi); **//Contains nothing: [] should contain [-2]**
                    System.out.println(x3Værdi); **//Contains [4, ., 6, -] should contain [3]**

                    double xResult = Double.parseDouble(String.join("", xVærdi)); //gets an error, because the arraylist is empty. 
                    double x2Result = Double.parseDouble(String.join("", x2Værdi));
                    double x3Result = Double.parseDouble(String.join("", x3Værdi));

这是SO post的另一个示例。

相关问题