Google API离线访问用户数据 [英] Google API Offline Access To User Data

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

问题描述

我正在研究一个需要权限访问用户Google日历的应用程序。我正在使用PHP客户端,并且能够离线访问这些日历。我的问题如下:一旦获得许可,我如何在以后访问特定用户的日历?如何在需要时为特定用户(已授予我访问日历的权限)创建客户端对象?

I'm working on an application that requires permission to access users Google Calendars. I'm using the PHP client and I'm able to get offline access to these calendars. My issue is as follows: once I have permission, how do I access the calendars for a specific users at a later date? How do I create a client object for a specific user (that has already given me permission to access their calendars) when I need it?

我已经设法使用 Google_Service_Oauth2 :: USERINFO_EMAIL 范围提取客户端ID,但我怎样才能使用它在晚些时候获取用户日历?这是甚至我该怎么处理它,或者我是朝着错误的方向前进?

I've managed to extract a client ID using the Google_Service_Oauth2::USERINFO_EMAIL scope but how could I use it to get that users calendars at a later date? Is that even how I should be going about it or am I heading in the wrong direction?

推荐答案

有几种方法可以采取实现这一点。在你的情况中,因为你只想访问用户日历,那么你应该包含在你的请求中的范围就是这里。您的客户端配置应该有 $ client-> setAccessType(offline); $ client-> setApprovalPrompt(force); / code>

There are several approaches you can take to achieve this. In your case, since you only want to access user calendar, then the scopes you should include in your requests are the one specified here. Your client configuration should have $client->setAccessType("offline"); and $client->setApprovalPrompt("force");

允许访问后,您将返回一个访问代码,您可以交换访问令牌。返回的访问令牌是您需要保存在数据库中的访问令牌。稍后,如果用户需要使用日历服务,那么您只需使用已保存的访问令牌。

After allowing access, you will be returned an access code that you can exchange for an access token. The access token returned is the one you need to save in a database. Later on, if the user needs to use the calendar service, you simply use the access token you already saved.

如果您需要代码细节,请发布代码细节我们无法完全理解您想要实现的内容,而无需查看您的实现。

If you need code specifics, then please post code specifics since we are not able to fully understand what you are trying to achieve without looking at your implementation.

以下面的代码片段为例。

Take for example the following code snippet.

/*
 * @$accessToken - json encoded array (access token saved to database)
*/

$client = new Google_Client();
$client->setAuthConfig("client_secret.json");
$client->addScope("https://www.googleapis.com/auth/calendar");

$_SESSION["access_token"] = json_decode($accessToken, true);

$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Calendar($client);

//REST OF THE PROCESS HERE

这篇关于Google API离线访问用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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