Google API和OAuth 2.0 [英] Google API and OAuth 2.0

查看:129
本文介绍了Google API和OAuth 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将google calendar API与php库一起使用,并且遇到用户向google api进行身份验证时遇到的问题.

I'm trying to use google calendar API with php library and i'm facing issues on the authentification of the user to the google api.

我有一个问题.我已经看到一些方法,您必须使用setDeveloperKey()方法将Api密钥/开发人员密钥设置为Google_Client对象,但是我也看到一些不需要这样做的人.有人可以告诉我这有什么区别吗?

I have a question. I've seen some come where you had to set the Api key / developer key to the Google_Client object with the method setDeveloperKey(), but i've also seen some people who don't. Could someone explain to me what difference does it make ?

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

The thing i'd like to do is to connect a user who have a google account to my application so he can add, list, remove, etc, events from a calendar. This is what i'm doing for the moment for the authentification :

$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内容的教程.我对令牌感到非常困惑,似乎没有人使用刷新令牌,对我来说这是必不可少的.但是也许我错了吗?

Does someone have a working commented code that i can analyse ? I can't find one that's working on the internet.. Or maybe a tutorial that explain everything about google api and oauth stuff. I'm so confused about tokens and nobody seems to use refresh tokens, and to me that's essential.. But maybe i'm wrong ?

感谢您的回答

推荐答案

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

I don't think you NEED to use setDeveloperKey I suspect that its only used for public APIs to enable you to use them but I haven't really tested it or thought about it before. I will have to look into that a bit more.

这是我用于通过Oauth2连接到Google日历的代码.从使用PHP访问Google日历– Oauth2 教程中直接删除

This is the code I use for connecting to Google Calendar with Oauth2. ripped directly from the Accessing Google Calendar with PHP – Oauth2 tutorial

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

这篇关于Google API和OAuth 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆