0

我正在尝试开始使用 google api(日历),但我无法完成。我的目的是以编程方式添加事件。授权应用程序后我收到消息“错误:无法接收访问令牌”。我配置了项目,访问令牌,允许写日历。这是我的 php 代码

<?php
session_start();

require_once('google-calendar-api.php');
require_once('settings.php');

// Google passes a parameter 'code' in the Redirect Url
if(isset($_GET['code'])) {
    try {
        $capi = new GoogleCalendarApi();
        
        // Get the access token 
        $data = $capi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);
        
        // Save the access token as a session variable
        $_SESSION['access_token'] = $data['access_token'];

        // Redirect to the page where user can create event
        header('Location: home.php');
        exit();
    }
    catch(Exception $e) {
        echo $e->getMessage();
        exit();
    }
}

?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">

#logo {
    text-align: center;
    width: 200px;
    display: block;
    margin: 100px auto;
    border: 2px solid #2980b9;
    padding: 10px;
    background: none;
    color: #2980b9;
    cursor: pointer;
    text-decoration: none;
}

</style>
</head>

<body>

<?php

$login_url = 'https://accounts.google.com/o/oauth2/auth?scope=' . urlencode('https://www.googleapis.com/auth/calendar') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online';

?>

<a id="logo" href="<?= $login_url ?>">Login with Google</a>

</body>
</html>
4

0 回答 0