Google API and OAuth 2.0


Google API and OAuth 2.0

我正在尝试将谷歌日历API与php库一起使用,但我面临着用户对谷歌API的身份验证问题。

我有一个问题。我见过有些人必须使用方法setDeveloperKey((将Api密钥/开发人员密钥设置为Google_Client对象,但我也看到有些人没有这样做。有人可以向我解释它有什么区别吗?

我想做的是将拥有Google帐户的用户连接到我的应用程序,以便他可以从日历中添加,列出,删除事件等。这是我目前为认证所做的:

$client = new Google_Client();
$client->setApplicationName("Test GCAL");
$client->setClientId($clientid);
$client->setClientSecret($clientsecret);
$client->setRedirectUri($callback_url);
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
$client->setScopes("https://www.googleapis.com/auth/calendar");
$service = new Google_Service_Calendar($client);

我做得对吗?

有人有我可以分析的工作注释代码吗?我找不到一个在互联网上工作的人。或者也许是一个教程,解释了关于谷歌 API 和 oauth 的东西的一切。我对令牌感到困惑,似乎没有人使用刷新令牌,对我来说这是必不可少的。但也许我错了?

感谢您的回答

我认为你不需要使用setDeveloperKey我怀疑它仅用于公共 API 以使您能够使用它们,但我以前没有真正测试过它或考虑过它。 我将不得不对此进行更多研究。

这是我用来通过Oauth2连接到谷歌日历的代码。 直接从使用 PHP 访问谷歌日历 – Oauth2 教程中翻录

<?php    
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';  
require_once 'CalendarHelper.php';  
session_start(); 
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("AIzaSyBBH88dIQPjcl5nIG-n1mmuQ12J7HThDBE");  
$client->setClientId('2046123799103-i6cjd1hkjntu5bkdkjj5cdnpcu4iju8p.apps.googleusercontent.com');
$client->setClientSecret('6s4YOx3upyJhtwnetovfK40e');
$client->setRedirectUri('http://localhost/google-api-php-client-samples/Calendar/oauth2Pure.php');
$client->setAccessType('offline');   // Gets us our refreshtoken
$client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly'));

//For loging out.
if (isset($_GET['logout'])) {
    unset($_SESSION['token']);
}

// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);  
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1:  The user has not authenticated we give them a link to login    
if (!isset($_SESSION['token'])) {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}    
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
    print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";  
    $service = new Google_Service_Calendar($client);    
    $calendarList  = $service->calendarList->listCalendarList();;
    print_r($calendarList);
    while(true) {
        foreach ($calendarList->getItems() as $calendarListEntry) {
            echo $calendarListEntry->getSummary()."<br>'n";
        }
        $pageToken = $calendarList->getNextPageToken();
        if ($pageToken) {
            $optParams = array('pageToken' => $pageToken);
            $calendarList = $service->calendarList->listCalendarList($optParams);
        } else {
            break;
        }
    }
}
?>