我已经阅读了有关如何使用Google API的文档,示例和教程,我已经运行了一个显示您最新活动和信息的迷你应用程序,但我使用会话来存储令牌.
我的问题是,如何从数据库中存储和检索令牌,以便当用户(已经注册)点击"登录"时,它可以立即使用API而无需重复授权?请注意,我使用该示例作为我的迷你应用程序的起点.
这是一段代码片段:
$client = new apiClient();
$client->setApplicationName(APP_NAME);
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URL);
$client->setDeveloperKey(DEV_KEY);
$plus = new apiPlusService($client);
$google_userinfo = new apiOauth2Service($client);
$message = "";
// In a real application this would be stored in a database, and not in the session!
if (isset($_SESSION['token']))
$client->setAccessToken($_SESSION['token']);
$_SESSION['token'] = $client->getAccessToken();
if (isset($_GET['code'])) {
$client->authenticate();
// In a real application this would be stored in a database, and not in the session!
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
...
//Somewhere …Run Code Online (Sandbox Code Playgroud)