douyangcheng4965 2017-12-07 08:52
浏览 83
已采纳

Google Api PHP客户端 - 如何登录

I'm trying to connect using the api of google using php to loggearme with the user of gmail and retrieve the calendars I have, for this I use this code. What I get is loggearme and that oauth2 I return the TOKEN but once I have this token I do not know how to use it to get the details of my google calendar.

Thank you

Settings.php

/* Google App Client Id */
define('CLIENT_ID', 'XXXXXXXX.apps.googleusercontent.com');

/* Google App Client Secret */
 define('CLIENT_SECRET', 'XXXXXXX');

 /* Google App Redirect Url */
 define('CLIENT_REDIRECT_URL', 
 'http://localhost:8081/googleTesting/oauth2callback.php');


    <html>
<head>....</head>

<body>



 <a href="<?= 'https://accounts.google.com/o/oauth2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online' ?>">Login with Google</a>
<br>

   </body>
 </html>

Calendar.php

//  We have access we can now create our service
if (isset($_SESSION['datosGoogle']['access_token'])) {

$client = new Google_Client();
$client->setAccessToken($_SESSION['datosGoogle']['access_token']);
print "LogOut";

$service = new Google_Service_Calendar($client);

$calendarList  = $service->calendarList->listCalendarList();;

while(true) {
    foreach ($calendarList->getItems() as $calendarListEntry) {

        echo $calendarListEntry->getSummary()."
";


        // get events
        $events = $service->events->listEvents($calendarListEntry->id);


        foreach ($events->getItems() as $event) {
            echo "-----".$event->getSummary()."";
        }
    }
    $pageToken = $calendarList->getNextPageToken();
    if ($pageToken) {
        $optParams = array('pageToken' => $pageToken);
        $calendarList = $service->calendarList->listCalendarList($optParams);
    } else {
        break;
    }
}

}

  • 写回答

1条回答 默认 最新

  • dpdhsq0783 2017-12-07 09:09
    关注

    When you request a login to google you add scope in that request.

    Google has better answer. Google tutorial,Google php implementation

    This part is important to highlight.

    //import libraries php composer.phar require google/apiclient:^2.0
    require_once __DIR__ . '/vendor/autoload.php';
    
    define('SCOPES', implode(' ', array(
      Google_Service_Calendar::CALENDAR_READONLY)
    ));
    $client->setScopes(SCOPES);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开