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;
    }
}

}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用google的api使用php连接到gge用户的loggearme并检索日历 我有,为此我使用这个代码。 我得到的是loggearme和oauth2我返回 TOKEN 但是一旦我有这个令牌我不知道怎么用它来获取我的细节 谷歌日历。

谢谢

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'); 
 
 
&lt; html&gt;  ; 
&lt; head&gt; ....&lt; / head&gt; 
 
&lt; body&gt; 
 
 
 
&lt; a href =“&lt;?='https://accounts.google.com  / o / oauth2 / auth?scope ='。urlencode('https://www.googleapis.com/auth/userinfo.profile')。'&amp; redirect_uri ='。urlencode(CLIENT_REDIRECT_URL  )。  '&amp; response_type = code&amp; client_id ='。  CLIENT_ID。  '&amp; access_type = online'?&gt;“&gt;使用Google登录&lt; / a&gt; 
&lt; br&gt; 
 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
 

Calendar.php

  //我们有权访问我们现在可以创建我们的服务
if(isset($ _ SESSION)  ['datosGoogle'] ['access_token'])){
 
 $ client = new Google_Client(); 
 $ client-&gt; setAccessToken($ _ SESSION ['datosGoogle'] ['access_token']); 
print  “LogOut”; 
 
 $ service = new Google_Service_Calendar($ client); 
 
 $ calendarList = $ service-&gt; calendarList-&gt; listCalendarList();; 
 
while(true){
 foreach  ($ calendarList-&gt; getItems()as $ calendarListEntry){
 
 echo $ calendarListEntry-&gt; getSummary()。“
”; 
 
 
 //获取事件
 $ events = $ service  - &gt; events-&gt; listEvents($ calendarListEntry-&gt; id); 
 
 
 foreach($ events-&gt; getItems()as $ event){
 echo“-----”。$  event-&gt; getSummary()。“”; 
} 
} 
 $ pageToken = $ calendarList-&gt; getNextPageToken(); 
 if($ pageToken){
  $ optParams = array('pageToken'=&gt;  $ pageToken); 
 $ calendarList = $ service-&gt; calendarList-&gt; listCalendarList($ optParams); 
} else {
 break; 
} 
} 
   \  n 
 

}

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题