我正在尝试使用Google API v3访问一个Google日历,并根据此处的文档:http://code.google.com/apis/calendar/v3/using.html#intro和here:https:// code.google.com/apis/console/,我需要的解决方案是"简单API访问"和"服务器应用程序密钥(带IP锁定)".
现在,当我使用以下代码创建页面时:
session_start();
require_once 'fnc/google-api-php-client/src/apiClient.php';
require_once 'fnc/google-api-php-client/src/contrib/apiCalendarService.php';
$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);
if (isset($_SESSION['oauth_access_token'])) {$apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
$token = $apiClient->authenticate();
$_SESSION['oauth_access_token'] = $token;
}
Run Code Online (Sandbox Code Playgroud)
在我的"config.php"文件中,我只添加了我的开发键(代替"X"):
global $apiConfig;
$apiConfig = array(
// True if objects should be returned by the service classes.
// False if associative arrays should be returned (default behavior).
'use_objects' => false,
// The application_name is included in the User-Agent HTTP header.
'application_name' => …Run Code Online (Sandbox Code Playgroud) 我在我的app.yaml配置文件中使用'login'选项来获取GAE应用程序.看起来像这样:
- url: /admin/.*
script: myapp.app
login: admin
- url: /.*
script: myapp.app
login: required
Run Code Online (Sandbox Code Playgroud)
更新(通过bossylobster的建议):我希望用户始终登录(未签名的用户无法做任何事情),我需要知道用户是谁.实际上,我需要OAuth2凭据才能与Google API进行通信(例如,我需要使用Google个人资料API获取一些用户信息,并使用Google Calendar API在用户的日历中写入).最后,我需要管理员用户执行某些操作(例如使用Google Provisioning API创建新域用户)
我正在使用google-api-client库,并且使用oauth2装饰器.然后,在我的RequestHandlers中,我有这个:
class MainHandler(webapp.RequestHandler):
@decorator.oauth_aware
def get(self):
if decorator.has_credentials():
# do something
else:
url = decorator.authorize_url()
self.response.out.write(template.render('templates/index.html',
{'authorize_url': url}))
Run Code Online (Sandbox Code Playgroud)
最后,我读到了另一种方法:
user = users.get_current_user()
if user:
# do something
else:
greeting = ("<a href=\"%s\">Sign in or register</a>." %
users.create_login_url("/"))
self.response.out.write("<html><body>%s</body></html>" % greeting)
Run Code Online (Sandbox Code Playgroud)
处理用户身份验证以满足我的需求的最佳方法是什么(请参阅更新)?
提前谢谢了
google-app-engine oauth-2.0 python-2.7 app.yaml google-api-python-client