Position:Home > Spark、Hadoop、Hive安装学习


up vote 3 down vote favorite

I need to make a PHP script that creates a sigle event on Google Calendar. I had no problems setting up client id, client secret, dev key and creating a new event.

My only problem is with OAuth2, in particular I need to make a permanent connection and I do not want to do the authentication everytime I run the script.

Actually, with this script I'm able to get a token and a refresh token, but every hour my token expires and I don't know how to refresh it. How can I edit this code to do that? Can I save both the token and the refresh token somewhere and always use the same data?

I obtain an uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant"

I have already read some other posts about this topic here in stackoverflow but I still haven't found a solution... :\


 require_once 'src/Google_Client.php';
 require_once 'src/contrib/Google_CalendarService.php';

 $client = new Google_Client();
 $client->setApplicationName("Calendar App");
 $cal = new Google_CalendarService($client);    

if (isset($_GET['logout'])) {

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

if (isset($_SESSION['token'])) {
    //echo $_SESSION['token'];
    $authObj = json_decode($_SESSION['token']);
    $accessToken = $authObj->access_token;
    $refreshToken = $authObj->refresh_token;
    $tokenType = $authObj->token_type;
    $expiresIn = $authObj->expires_in;
    echo 'access_token = ' . $accessToken;
    echo '<br />';
    echo 'refresh_token = ' . $refreshToken;
    echo '<br />';
    echo 'token_type = ' . $tokenType;
    echo '<br />';
    echo 'expires_in = ' . $expiresIn;


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

// Creation of a single event
$event = new Google_Event();


Thanks a lot for your support!

php google-api oauth-2.0 google-calendar google-oauth share | improve this question edited Nov 13 '13 at 16:32 asked Nov 7 '13 at 22:28 Roberto Milani 249 1 4 17 add a comment  | 

3 Answers 3

active oldest votes up vote 5 down vote

This page https://developers.google.com/accounts/docs/OAuth2WebServer#offline explains how the refresh token works, and how to use it to get a fresh access token using raw http.

From this question How to refresh token with Google API client? here is the php equivalent

here is the snippet to set token, before that make sure the access type should be set to offline

if (isset($_GET['code'])) {
  $_SESSION['access_token'] = $client->getAccessToken();
To refresh token

$google_token= json_decode($_SESSION['access_token']);
this will refresh your token, you have to update it in session for that you can do

 $_SESSION['access_token']= $client->getAccessToken()

DEBUGGING Follow these three steps to debug any oauth application

  1. Make sure you have read https://developers.google.com/accounts/docs/OAuth2 and also the appropriate sub-page (webapp, javascript, etc) depending on which flavour of oauth you are using. You won't get very far debugging your app if you don't understand what it's doing.

    Recommend:hadoop - how to query Hive from Spark on CDH 5.4.4

    a Spark dataframe. Looking at the documentation, it suggests that we can do the following: from pyspark.sql import HiveContextsqlContext = HiveContext(sc)results = sqlContext.sql("SHOW TABLES").collect() ... providin

  2. Use the Oauth Playground at https://developers.google.com/oauthplayground/ to walk through the Oauth steps and ultimately make a Google API call. This will confirm your understanding and show you what the http calls and responses look like

  3. Use tracing/logging/proxy etc to trace the actual http traffic from your app. Compare this with what you observed in step 2. This should show you where the problem lies.

share | improve this answer edited Nov 12 '13 at 15:35 answered Nov 8 '13 at 6:32 pinoyyid 9,936 6 26 71      Thanks for your answer but I'm still stuck on the problem. Now I got an 'Error refreshing the OAuth2 token, error" invalid_request" } I've edited the initial code this way: if (isset($_GET['code'])) { $client->authenticate(); $_SESSION['access_token'] = $client->getAccessToken(); header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); $google_token= json_decode($_SESSION['access_token']); $client->refreshToken($google_token->refresh_token); $_SESSION['access_token']= $client->getAccessToken(); } –  Roberto Milani Nov 12 '13 at 14:30      and before all I've defined an hardcoded JSON with a token and its refresh token this way. $arr = array('access_token' => 'yz...', 'refresh_token' => 'xyz', 'token_type' => 'Bearer', 'expires_in' => 3600, 'created' => ...); $_SESSION['token'] = json_encode($arr); I dont understand when I have to refresh the token. Thanks in advance –  Roberto Milani Nov 12 '13 at 14:30      I'm not going to look at your code (I don't know php). I've updated my answer with some tips on how to debug this kind of problem –  pinoyyid Nov 12 '13 at 15:29      @Roberto: Did you ever find a fix for the "Error refreshing the OAuth2 token"? I am having the same issue. Thanks! –  jftuga May 29 '14 at 15:43 add a comment  |  up vote 2 down vote

I got this error before because I was trying to authenticate twice.

Since you have this line:

if (isset($_GET['code']))

It will try to authenticate when the code is in the query string regardless of whether you're already authenticated. Try checking whether the SESSION token is present before trying to (re)authenticate.

share | improve this answer answered Dec 9 '13 at 23:45 thayne 55 8      Thx. This answer fixed problem for me. –  stamp Sep 29 '14 at 9:04 add a comment  |  up vote 0 down vote

Try removing setDeveloperKey(), this was causing some issues for me and it seems unneeded.

share | improve this answer answered Jun 18 '14 at 23:58 stealthysnacks 170 1 3 13 add a comment  | 

Your Answer

  draft saved draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name Email

Post as a guest

Name Email discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged php google-api oauth-2.0 google-calendar google-oauth or ask your own question.

Recommend:hadoop - Spark as an engine for Hive

nd would like to use Spark with Hive. Best, hadoop hive apache-spark bigdata share | improve this question asked Mar 3 '15 at 21:42 Raj baba 36 6 Hive on Spark is still a work in progress. You can track it here: HIV

Recommend:Hadoop 2.7, Spark, Hive, JasperReports, Scoop - Architecuture

hat I'm asking is for an advice on how the architecture should be designed. What I'm planning to do is develop a reporting platform using existing data. Following is data I gathering by researching. I have an existing RD

Your Answer
(Ctrl+Enter To Post)   
    Copyright © 2015-2017 163JAVA.COM All Rights Reserved.