我已閱讀有關如何使用Google API的文檔,示例和教程,我已經有一個已經運行的迷你應用程序來顯示您的最新活動和信息,但我使用會話來存儲令牌。Google+ OAuth 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>
非常感謝你的幫助!
問候,
約翰
感謝您的回答!這很有用,我可以將它用作臨時修復。:)我已經檢查過它。但是你知道或者有任何關於如何存儲我在$ client-> getAccessToken()中獲得的令牌的帖子的鏈接嗎?並在用戶登錄時使用它們? – user1239714 2012-04-12 03:03:12
@ user1239714:我不知道有一個特定的地方可以將您的建議鏈接到此處。這完全取決於您如何在應用程序中保留其餘數據。例如,如果您正在編寫典型的LAMP應用程序,則可以將刷新標記存儲在MySql中以便稍後調用。 – mimming 2012-04-13 00:35:19
@JennyMurphy「你可以在源代碼中看到這個」。鏈接被破壞。你能否提供另一個樣本 – knightrider 2013-01-17 17:17:44