我建立一个SPA使用vue.js具有PHP后端服务器(超薄框架3)。这是两个独立的项目,留在两台不同的服务器上,后端根本没有前端。使服务器对服务器的请求,谷歌日历API
SPA(vue.js)通过ajax发出后端请求。
现在我想实施谷歌日历API创建一个日历和事件每次用户创建一个待办事项。为此,我需要服务器到服务器访问Google Calendar API(即使用户未登录,我可能需要更改GCAL上的事件)。
我想要了解的是,如何使用Google JS库使用vue.js获取访问令牌(和刷新令牌)并将其保存在数据库中,以便我的后端可以使用它来向GCAL发出离线请求API。
当我使用JS库的Oauth v.2时,我所得到的只是access_token,它不能用于服务器到服务器的通信。
[UPDATE]
好,有点多的信息。我从Google以下的指南和我的前端看起来像这样的时刻 jsbin
这样我就可以成功授权用户访问和使用JavaScript SDK他们的日历。但令牌的Javascript SDK返回是这样的
{
_aa: "1"
access_token: "xxxxxxx"
client_id: "yyyyyyyyyy"
cookie_policy: undefined
expires_at: "1456400189"
expires_in: "3600"
g_user_cookie_policy: undefined
issued_at: "1456396589"
response_type: "token"
scope: "https://www.googleapis.com/auth/calendar"
state: ""
status: Object
google_logged_in: false
method: "AUTO"
signed_in: true
token_type: "Bearer"
}
我这个令牌发送到我的后端服务器,并尝试做出GCAL API的请求如下
$token = $request->getParam('token');
$client = new Google_Client();
$client->setApplicationName('Web');
$client->setScopes([Google_Service_Calendar::CALENDAR]);
$client->setAuthConfigFile(ROOT_DIR . '/client_secret.json');
$client->setAccessType('offline');
$client->setAccessToken(json_encode($token));
$service = new Google_Service_Calendar($client);
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
并返回错误说令牌已过期。我检查了谷歌代码,并发现了原因,返回此错误是因为这些线路的
public function isAccessTokenExpired()
{
if (!$this->token || !isset($this->token['created'])) {
return true;
}
// If the token is set to expire in the next 30 seconds.
$expired = ($this->token['created']
+ ($this->token['expires_in'] - 30)) < time();
return $expired;
}
正如你可以看到来自前端没有created
领域的令牌以及没有refresh_token
场。
很想帮忙,但你的问题很广泛。请发布你写的代码,你得到的结果以及你想得到的结果。然后我们可以弄清楚什么是错误的。 – Jeff