刷新令牌与谷歌API客户端PHP [英] refresh token with google api client php

查看:203
本文介绍了刷新令牌与谷歌API客户端PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对google api的痛苦继续...



我可以使用脚本来使用刷新令牌 - 自动刷新并保持对日历的访问活。我可以与日历互动,一切都很好。

我现在想继续使用PHP的google api客户端,并且昨天我的代码是有效但现在已过期我无法弄清楚如何使用刷新令牌来获取新令牌并在将来到期时继续进行验证。



I已经找到了很多引用并试用了这些 - 这里是我的基于simple.php示例的测试脚本:

 << ;?php 
require_once'../../src/Google_Client.php';
require_once'../../src/contrib/Google_CalendarService.php';
session_start();

$ client = new Google_Client();
$ client-> setAccessType('offline');
$ client-> setApplicationName(Downs Golfmanager);


//访问https://code.google.com/apis/console?api=calendar以生成您的
//客户端ID,客户端密钥并注册你的重定向uri。
$ client-> setClientId('XXXXXX.apps.googleusercontent.com');
$ client-> setClientSecret('XXXXXX');
$ client-> setRedirectUri('XXXXXX');
$ client-> setDeveloperKey('XXXXXX');
$ client-> refreshToken('X / XXXXX');


$ client-> setUseObjects(true);
$ cal = new Google_CalendarService($ client);
if(isset($ _ GET ['logout'])){
unset($ _ SESSION ['token']);


if(isset($ _ GET ['code'])){
$ client-> authenticate($ _ GET ['code']);
$ _SESSION ['token'] = $ client-> getAccessToken();
header('Location:http://'。$ _SERVER ['HTTP_HOST']。$ _SERVER ['PHP_SELF']);


if(isset($ _ SESSION ['token'])){
$ client-> setAccessToken($ _ SESSION ['token']);

$ b $ if($ client-> isAccessTokenExpired()){
/
echo'访问令牌已过期'; // Debug
// $ client-> refreshToken($ refreshToken);


if($ client-> getAccessToken()){

$ event = new Google_Event();

$ event-> setSummary('Appointment');
$ event-> setLocation('Somewhere');

$ start = new Google_EventDateTime();
$ start-> setDateTime('2013-08-28T14:00:00.000 + 01:00');
$ event-> setStart($ start);

$ end = new Google_EventDateTime();
$ end-> setDateTime('2013-08-28T15:25:00.000 + 01:00');
$ event-> setEnd($ end);

$ createdEvent = $ cal-> events-> insert('XXXXXX@group.calendar.google.com',$ event);

// echo< pre> 。 print_r($ createdEvent,true)。 < /预> 中;
echo $ createdEvent-> getId();


//拉动事件
$ events = $ cal-> events-> listEvents('XXXXXX@group.calendar.google.com');

while(true){
foreach($ events-> getItems()as $ event){
echo $ event-> getSummary();
}
$ pageToken = $ events-> getNextPageToken();
if($ pageToken){
$ optParams = array(
'pageToken'=> $ pageToken,
'timeMin'=>'2013-08-26T00:00 :00 + 01:00',
'timeMax'=>'2013-08-31T00:00:00 + 01:00'
);
$ events = $ cal-> events-> listEvents('XXXXXX@group.calendar.google.com',$ optParams);
} else {
break;
}
}
//打印< h1>事件列表< / h1>< pre> 。 print_r($ events,true)。 < /预> 中;



$ calList = $ cal-> calendarList-> listCalendarList();
print< h1>日历列表< / h1>< pre> 。 print_r($ calList,true)。 < /预> 中;


$ _SESSION ['token'] = $ client-> getAccessToken();
} else {
$ authUrl = $ client-> createAuthUrl();
print< a class ='login'href ='$ authUrl'>连接我!< / a>;

$ / code>

与我以前的帖子一样,我不清楚这里应该怎么做。我的令牌已过期 - 我有刷新令牌。编辑:错误代码是: OAuth 2.0访问令牌已过期,并且刷新令牌不可用



我已经注释了一些额外的行,试图解决,只是为了避免混淆进一步的消息。

谢谢

解决方案

<好的 - 基于一些帖子,我已经解决了获取刷新令牌以获得新访问令牌的问题。在开始尝试访问之前,我使用了这段代码:

  if($ client-> isAccessTokenExpired()){
回显'访问令牌已过期'; // Debug
$ client-> refreshToken('X / XXXXXX');
}

其中 refreshToken 是我的刷新令牌。我将代码硬编码到代码中。

这可能不是正确的解决方案,因为我已经看到了将密钥存储在数据库中的引用,所以大概我可以存储在一个数据库中的当前访问令牌与刷新令牌,并拉动这些形式的数据库,当需要保存硬编码他们?

希望这种方法,我用过到期后再次工作 - 手指划过!


My pain with google api continues...

I'm able to use a script to use a refresh token - automatically refresh and keep my access to a calender alive. I can interact with the calendar and all is good.

I now want to move on to using the google api client for php and I had this working yesterday whilst my token was valid but now this has expired I can't work out how to use the refresh token to get a new token and to keep authenticating once it expires in the future.

I've found a number of references and tried these - here's my test script so far based on the simple.php example:

<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
session_start();

$client = new Google_Client();
$client->setAccessType('offline');
$client->setApplicationName("Downs Golfmanager");


// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('XXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXX');
$client->setRedirectUri('XXXXXX');
$client->setDeveloperKey('XXXXXX');
$client->refreshToken('X/XXXXX');


$client->setUseObjects(true);
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if($client->isAccessTokenExpired()) {
    /
    echo 'Access Token Expired'; // Debug
    //$client->refreshToken($refreshToken);
}

if ($client->getAccessToken()) {

    $event = new Google_Event();

    $event->setSummary('Appointment');
    $event->setLocation('Somewhere');

    $start = new Google_EventDateTime();
    $start->setDateTime('2013-08-28T14:00:00.000+01:00');
    $event->setStart($start);

    $end = new Google_EventDateTime();
    $end->setDateTime('2013-08-28T15:25:00.000+01:00');
    $event->setEnd($end);

    $createdEvent = $cal->events->insert('XXXXXX@group.calendar.google.com', $event);

    //echo "<pre>" . print_r($createdEvent, true) . "</pre>";
    echo $createdEvent->getId();


    //test pulling events
    $events = $cal->events->listEvents('XXXXXX@group.calendar.google.com');

    while(true) {
      foreach ($events->getItems() as $event) {
        echo $event->getSummary();
      }
      $pageToken = $events->getNextPageToken();
      if ($pageToken) {
        $optParams = array(
            'pageToken' => $pageToken,
            'timeMin'=>'2013-08-26T00:00:00+01:00',
            'timeMax'=>'2013-08-31T00:00:00+01:00'
            );
        $events = $cal->events->listEvents('XXXXXX@group.calendar.google.com', $optParams);
      } else {
        break;
      }
    }
    //print "<h1>Events List</h1><pre>" . print_r($events, true) . "</pre>";



  $calList = $cal->calendarList->listCalendarList();
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";


$_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

As with my previous posts I'm not clear what should be going on here. My token has expired - I have the refresh token. what changes to my code do I need to automatically refresh authorisation each time I access.

Edit: The error code is: The OAuth 2.0 access token has expired, and a refresh token is not available

I've commented out a couple of additional lines above which I'd included in my attempts to solve just to try and avoid confusing further

Thank you

解决方案

OK - Based on a couple of posts I've fixed the problem of getting the refresh token to get a new access token. I used this code before I started to attempt any access:

if($client->isAccessTokenExpired()) {
    echo 'Access Token Expired'; // Debug
    $client->refreshToken('X/XXXXXX');
}

Where the blanked out part in refreshToken is my refresh token. I hard coded the key into the code.

This is probably not the correct solution as I've seen references to storing the key in a database, so presumably I could store the current access token in a db with the refresh token and pull these form the db as and when required to save hard coding them?

Hopefully this method I've used will work again after it expires - fingers crossed!

这篇关于刷新令牌与谷歌API客户端PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆