我已阅读有关如何使用 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 here, I added a function that ties $_SESSION['token'] to the user's info.
...
<form action="" method="post" id="form1" name="form1">
<fieldset>
<div id="main-url-section" style="width: 50%;margin: 0px auto;text-align: center;">
<?php
$authUrl = $client->createAuthUrl();
print "<p><a class='login' href='$authUrl'>Log me in!</a></p>";
?>
</div>
</fieldset>
</form>
非常感谢你的帮助!
问候,
约翰