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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀