【问题标题】:Google API and OAuth 2.0谷歌 API 和 OAuth 2.0
【发布时间】:2015-02-14 12:31:05
【问题描述】:

我正在尝试将 google 日历 API 与 php 库一起使用,但在用户对 google api 的身份验证方面遇到了问题。

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

我想做的事情是将拥有谷歌帐户的用户连接到我的应用程序,以便他可以添加、列出、删除等日历中的事件。这就是我目前正在做的身份验证:

$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);

我做得对吗?

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

感谢您的回答

【问题讨论】:

    标签: php oauth-2.0 google-calendar-api google-oauth google-api-php-client


    【解决方案1】:

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

    这是我用于通过 Oauth2 连接到 Google 日历的代码。直接摘自Accessing Google Calendar with 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;
            }
        }
    }
    ?>
    

    【讨论】:

    • 感谢您的回答!我会试试然后告诉你。 (虽然隐藏您的应用程序信息!您不应该那样显示它:p)但是您似乎没有使用刷新令牌,这正常吗?您的重定向 uri 是否指向此页面?
    • 该应用程序信息已更改,但只有我知道它的更改如何让我在处理教程时更轻松:) 重定向 URI 是在开发人员控制台中设置的。我在编写教程时使用 localhost。客户端库处理所有刷新令牌的东西。如果您 p​​rint_r 会话令牌,认为您可以看到它
    • 好的..它正在工作。太感谢了 !我将通过将所有这些放在一个类中来做一些改变。实际上,这很简单..我已经做了好几天了啊哈哈!
    • 如果你在寻找开发者密钥的过程中发现了一些有趣的东西,我会很高兴你与我分享!
    • 卡在其他任何事情上让我知道我接受教程请求:)
    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 2017-07-31
    • 1970-01-01
    • 2015-01-10
    • 2012-11-13
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多