允许我的用户使用带有PHP的Google Calendar API在我的日历中添加会议而无需身份验证 [英] allow my user to add meeting in my calendar with Google Calendar API with PHP without auth
问题描述
我正在开发一个应用程序,该应用程序允许我的用户使用我的Google日历之一来计划/重新安排日历事件,而我不需要用户通过Google进行身份验证.我需要使用带有服务帐户的Google Calendar API.
I am working on a app that allows my users to schedule/reschedule Calendar events using one of my Google calendars, where I don’t need the users to authenticate themselves with Google. What i need to use is the Google Calendar API with a service account.
我试图用api v2编写东西,但是遇到了问题,所以我想知道您是否可以将我引导到一个教程或一个示例,或者只是一个插入示例
I tried to write something with api v2 but I had a problem, so I would like to know if you could direct me to a tutorial or an example, or just an example of insertion
<?php
require_once './google-api-php-client-2.2.2/vendor/autoload.php';
session_start();
/************************************************ */
$client_id = '751416246659ae431430560098c25ba433c3e483';
$Email_address = ' testecalendar@spartan-grail-241202.iam.gserviceaccount.com';
$key_file_location = './credentials.json';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);
// separate additional scopes with a comma
$scopes = "https://www.googleapis.com/auth/calendar.readonly";
$client->setAuthConfig('./credentials.json');
$service = new Google_Service_Calendar($client);
?>
<html>
<body>
<?php
$calendarList = $service->calendarList->listCalendarList();
while (true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary() . "<br>\n";
// get events
$events = $service->events->listEvents($calendarListEntry->id);
foreach ($events->getItems() as $event) {
echo "-----" . $event->getSummary() . "<br>";
}
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
?>
我收到此错误:
严重错误:未捕获到的Google_Service_Exception:{"error":{"errors":[{"domain":"global","reason":"required","message":需要登录","locationType":/Users/macbook/Desktop/testcal/google-api-php-client-2.2中的标题",位置":授权"}],代码":401,消息":需要登录"}}. 2/src/Google/Http/REST.php:118堆栈跟踪:#0/Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php(94 ):Google_Http_REST :: decodeHttpResponse(Object(GuzzleHttp \ Psr7 \ Response),Object(GuzzleHttp \ Psr7 \ Request),'Google_Service _...')#1/Users/macbook/Desktop/testcal/google-api-php-client -2.2.2/src/Google/Task/Runner.php(176):Google_Http_REST :: doExecute(Object(GuzzleHttp \ Client),Object(GuzzleHttp \ Psr7 \ Request),'Google_Service _...')#2/Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php(58):Google_Task_Runner-> run()#3/Users/macbook/Desktop/testcal/google -api-php-client-2.2.2/src/Google/Client.php(798):Google_Http_第118行的/Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php中的RES
Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } } in /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php:118 Stack trace: #0 /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php(58): Google_Task_Runner->run() #3 /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Client.php(798): Google_Http_RES in /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php on line 118
推荐答案
无需用户同意即可从PHP应用程序访问Google Calendar API屏幕
不确定您是否真的要经历所有这一切(只有一次:-),但是就在这里...
Not sure if you really want to go through all this ( it's only once :-) ), but here your go...
服务帐户是一种特殊的Google帐户,旨在代表需要认证并有权访问Google API中数据的非人类用户."
"A service account is a special type of Google account intended to represent a non-human user that needs to authenticate and be authorized to access data in Google APIs."
该想法是使用服务帐户"(电子邮件地址)授权/共享您的日历,并使用服务帐户密钥"进行身份验证.
The idea is to authorize/share your calendar with a "Service account" (an email address) and authenticate with a "Service account key".
我将从代码开始:
<?php
// read google calendar from php command line application
require_once './google-api-php-client-2.2.3/vendor/autoload.php';
$client = new Google_Client();
$client->addScope("https://www.googleapis.com/auth/calendar.readonly");
$client->setAuthConfig('client_credentials.json');
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary();
echo "\n------------------------------\n\n";
// get events
$events = $service->events->listEvents($calendarListEntry->id);
foreach ($events->getItems() as $event) {
echo "- " . $event->getSummary() . "\n";
echo "- " . $event->getStart()->getDateTime() . "\n\n";
}
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
如何使其工作:
从此存储库中( https://github.com/googleapis/google -api-php-client/releases )
在您的php应用程序的根目录中下载"google-api-php-client-2.2.3.zip"并解压缩"google-api-php-client-2.2.3".
From this repo (https://github.com/googleapis/google-api-php-client/releases)
download "google-api-php-client-2.2.3.zip" and unzip "google-api-php-client-2.2.3" in the root of your php application.
接下来,您需要此"client_credentials.json" 文件.您可以从"Google Cloud Platform"获得它.最好是创建一个新项目,您可以使用现有项目,但是可能无法获得我在下面描述的内容(您可能能够做到,其本质是相同的.)
Next, you need this "client_credentials.json" file. You'll get it from "Google Cloud Platform". Best would be to create a new project, you could use an existing project but you might not get what I describe below (you might be able to make it, the essence is the same).
创建并选择新项目后,
-转到"API和服务">库"
-搜索日历",然后选择"Google Calendar API"
-启用
After you create and select a new project,
- go to "APIs & Services" > "Library"
- search for "Calendar" then select "Google Calendar API"
- ENABLE
接下来,您需要创建凭证"
-如果您尚未重定向,请转到"API和服务">凭据"
-点击创建凭据">帮助我选择"
-他们会询问您您正在使用哪个API",选择"Google Calendar API"
-接下来,他们会询问您您将从何处调用API?",然后选择其他UI(例如Windows,CLI工具)"
-接下来,他们会问您您将访问哪些数据?",选择应用程序数据"
-接下来,点击我需要什么凭证?"
Next, you need to "CREATE CREDENTIALS"
- if you're not already redirected go to "APIs & Services" > "Credentials"
- click on "Create credentials" > "Help me choose"
- they'll ask you "Which API are you using", choose "Google Calendar API"
- next, they'll ask you "Where will you be calling the API from?", choose "Other UI (e.g. Windows, CLI tool)"
- next, they'll ask you "What data will you be accessing?", pick "Application data"
- next, click on "What credentials do I need?"
现在,您位于向项目添加凭据"页面中
-填写服务帐户名称"
-选择项目">编辑器"中的角色"
-键类型"应为"JSON"
-单击继续",系统将提示您下载此"something-something.json"文件(您可以将其保存在php应用程序的根目录中)
Now you're on "Add credentials to your project" page
- fill "Service account name"
- select "Role" of "Project" > "Editor"
- "Key type" should be "JSON"
- click "Continue" and you'll be prompted to download this "something-something.json" file (you can save it in the root of your php application)
接下来,在您的php应用程序文件夹中,将"something-something.json"重命名为"client_credentials.json"
Next, in your php application folder, rename the "something-something.json" to "client_credentials.json"
快到了...
接下来,使用文本编辑器打开"client_credentials.json"
-复制作为"client_email"值的电子邮件地址
类似于"test-php-app@xxxxxxxxxx-nnnnnnnnnnnnnn.iam.gserviceaccount.com"
Next, open "client_credentials.json" with a text editor
- copy the email address that is the value of "client_email"
something like "test-php-app@xxxxxxxxxx-nnnnnnnnnnnnn.iam.gserviceaccount.com"
接下来,转到您的日历(您要访问的Google日历),然后转到设置">日历设置"
-向下滚动到与特定的人共享"(您会在列表中看到自己)
-点击"+加人",然后粘贴电子邮件地址,例如"test-php-app@xxxxxxxxxx-nnnnnnnnnnnnnnn.iam.gserviceaccount.com"
-选择您想要的权限"
-点击发送"按钮
Next, go to your calendar (your Google Calendar, the one you want to access) and go to "Settings" > "Calendar settings"
- scroll down to "Share with specific people" (you'll see yourself in the list)
- click on "+ Add people" and paste the email address the one like "test-php-app@xxxxxxxxxx-nnnnnnnnnnnnn.iam.gserviceaccount.com"
- select what you'd like for "Permissions"
- click on "Send" button
现在,通过服务帐户,您的php应用程序可以访问您的日历了.
Now your php application via the service account has access to your calendar.
以下是显示日历ID的代码:
Here is the code to show the calendar id:
echo "calendar summary: " . $calendarListEntry->getSummary();
echo "\ncalendar id: " . $calendarListEntry->getId();
echo "\n------------------------------\n\n";
这篇关于允许我的用户使用带有PHP的Google Calendar API在我的日历中添加会议而无需身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!