dro62273 2014-11-27 17:58
浏览 22

谷歌日历API v3(php)使用刷新令牌创建新的访问令牌,然后将新事件发布到日历中

I have been working through the docs, and so far, I have managed to add a new event to my calendar, using my authorization credentials from the Google Developers Console and the code snippet here

https://developers.google.com/google-apps/calendar/v3/reference/events/insert

<?php


$client = new Google_Client();
// OAuth2 client ID and secret can be found in the Google Developers Console.
$client->setClientId('xxxxx.apps.googleusercontent.com');
$client->setClientSecret(xxxxx);
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->addScope('https://www.googleapis.com/auth/calendar');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');


$service = new Google_Service_Calendar($client);


$authUrl = $client->createAuthUrl();


//Request authorization
print "Please visit:
$authUrl

";
print "Please enter the auth code:
";


//$authCode = trim(fgets(STDIN));
$authCode = '';


// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);


$client->setAccessToken($accessToken);


$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment);
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-12-22T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-12-22T10:00:00.000-08:00');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail(xxx@gmail.com');
// ...
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('xxxxx@group.calendar.google.com', $event);


echo $createdEvent->getId();




?>

This works, but I need to get a new OAuth2.0 access token every time. What I want to do is use a refresh token to automatically generate a new access token, so I can add new gcal events from my web page

If I run the following code, I can successfully regenerate a new access token using the refresh token

<?php


function getAccessToken(){
    $tokenURL = 'https://accounts.google.com/o/oauth2/token';
    $postData = array(
        'client_secret'=>'xxxxx',
        'grant_type'=>'refresh_token',
        'refresh_token'=>'xxxxx',
        'client_id'=>'xxxx.apps.googleusercontent.com'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $tokenURL);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//need this otherwise you get an ssl error
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tokenReturn = curl_exec($ch);
    $token = json_decode($tokenReturn);
    //var_dump($tokenReturn);
    $accessToken = $token->access_token;

    return ($accessToken);
}
?>

This always gives me a new access token, but if I put this token into the code at the top of the page, I get an error. Google_Auth_Exception' with message 'The OAuth 2.0 access token has expired, and a refresh token is not available

I have tried returning the access token only, the return value, the json decoded return value, but I always get an error of some sort

Can anyone point me in the right direction, or to a code snippet which will allow me to insert a new event into my calendar ?

Thanks

Thanks for the link SGC

I think the reason for the initial failure, is that the function to create a new access token using the refresh token only returns 3 values, the 'access_token' itself, the 'token_type', and 'expires_in'.

What is also required when calling setAccessToken() is the 'refresh_token' and the 'created' value.

I had the refresh token already, but I am not clear on the 'created', is it when the refresh token was created or the access token, and will this value change ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏