cr1*_*1zz 6 php api ajax cross-domain
我正在使用Google API通过OAuth访问我的日历条目.不幸的是我收到以下错误(服务器是本地raspi):
无法加载https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=online&client_id=**** - ****.apps.googleusercontent.com&redirect_uri = http%3A%2F%2Fopenhabianpi..%2Fsmarthome%2Fphp%2Fscripts%2Fscript.oauth2callback.php&state&scope = https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.readonly&approval_prompt = auto:对预检请求的响应未通过访问控制检查:否'访问控制-Allow-Origin'标头出现在请求的资源上.起源' http:// openhabianpi..因此,不允许访问.响应具有HTTP状态代码405.
我的脚本:
Ajax请求
var termine = function (){
$.ajax({
type: "POST",
url: "php/ajax/ajax.termine.php",
data: {
action: 'get_termine'
},n
success: function(response) {
console.log(response);
}
});
}
Run Code Online (Sandbox Code Playgroud)
ajax.termine.php
require dirname(dirname(__FILE__)).'/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig(dirname(dirname(__FILE__)).'/config/client_secret.json');
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$service = new Google_Service_Calendar($client);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n", $event->getSummary(), $start);
echo date('c');
}
}
} else {
$redirect_uri = 'http://openhabianpi.***.***/smarthome/php/scripts/script.oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Run Code Online (Sandbox Code Playgroud)
script.oauth2callback
<?php
require_once dirname(dirname(__FILE__)).'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile(dirname(dirname(__FILE__)).'/config/client_secret.json');
$client->setRedirectUri('http://openhabianpi.***.***/smarthome/php/scripts/script.oauth2callback.php');
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://openhabianpi.***.***/smarthome/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我尝试了以下内容:
dataType:'jsonp',
header("Access-Control-Allow-Origin:*");
在.htaccess或apache.conf中设置
Access-Control-Allow-Origin"*"
在此先感谢您的帮助!
以下脚本通过 ajax 运行(您必须为此使用服务帐户):
<?php
/**
* Session aufbauen und user check
*/
session_start();
require dirname(dirname(__FILE__)).'/vendor/autoload.php'; //Google API via Composer
$json_path = '***PATH to service account credential JSON***';
$client = new Google_Client();
$client->setAuthConfig($json_path);
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$client->setApplicationName("***Your Appname***");
$service = new Google_Service_Calendar($client);
$calendarId = '***Your CAL ID***';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print "";
} else {
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
$date = date("d.m.Y", strtotime($start));
$time = date("G:i", strtotime($start));
$today = date("d.m.Y");
if ($date == $today){
$array[] = array(
"datum" => $date,
"time" => $time,
"termin" => $event->summary
);
}
}
}
echo json_encode($array);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
373 次 |
| 最近记录: |