Google APIの使用方法に関するドキュメント、サンプル、チュートリアルを読んでいます。私はあなたの最新の活動や情報を示すミニアプリを既に実行していますが、セッションを使用してトークンを格納します。Google+ OAuth 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
@ user1239714:ここではアドバイスのためのリンク先がわかりません。これは、アプリケーションの残りのデータを永続化する方法に完全に依存します。たとえば、典型的なLAMPアプリケーションを作成している場合は、リフレッシュトークンをMySqlに保存して後で呼び出すことができます。 – mimming
@JennyMurphy「ソースコードでこれを見ることができます」。リンクが壊れています。別のサンプルを提供してください。 – knightrider