oauth認証でグーグルのログインについて
localhostからのログイン認証に成功したのですが、サーバー上でのグーグルログインができません
ソースコードはこちらです
コード <?php require_once("../dbconnect.php"); dbConnect(); $AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; $params = array( 'client_id' => '', 'redirect_uri' => '', 'scope' => 'profile email', 'response_type' => 'code', 'access_type' => 'offline' ); header("Location: " . $AUTH_URL. '?' . http_build_query($params)); ?>
<?php // データベース接続 require_once("../dbconnect.php"); dbConnect(); // アプリケーション設定 define('CONSUMER_KEY', ''); define('CONSUMER_SECRET', ''); define('CALLBACK_URL', ''); // URL define('TOKEN_URL', 'https://accounts.google.com/o/oauth2/token'); define('INFO_URL', 'https://www.googleapis.com/oauth2/v1/userinfo'); $params = array( 'code' => $_GET['code'], 'grant_type' => 'authorization_code', 'redirect_uri' => CALLBACK_URL, 'client_id' => CONSUMER_KEY, 'client_secret' => CONSUMER_SECRET, ); // POST送信 $options = array('http' => array( 'method' => 'POST', 'content' => http_build_query($params) )); // アクセストークンの取得 $res = file_get_contents(TOKEN_URL, false, stream_context_create($options)); // レスポンス取得 $token = json_decode($res, true); if(isset($token['error'])){ echo 'エラー発生'; exit; } $access_token = $token['access_token']; $params = array('access_token' => $access_token); // ユーザー情報取得 $res = file_get_contents(INFO_URL . '?' . http_build_query($params)); $result = json_decode($res, true); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="style.css"> <title>Googleログイン</title> </head> <header> <h1>TravelSupport</h1> <h2>ホーム</h2> </header> <body> <h3>プロフィール画像</h3> <img src="<?php echo $result['picture']; ?>" width="80"> <h3>ユーザー情報</h3> <table> <tr><td>ID</td><td><?php echo $result['id']; ?></td></tr> <tr><td>ユーザー名</td><td><?php echo $result['name']; ?></td></tr> <tr><td>苗字</td><td><?php echo $result['family_name']; ?></td></tr> <tr><td>名前</td><td><?php echo $result['given_name']; ?></td></tr> <tr><td>Email</td><td><?php echo $result['email']; ?></td></tr> </table> </body> </html>
redirect_uriはipアドレスにしています。
サーバー上でのログインを実装したいです。
よろしくお願いします。
あなたの回答
tips
プレビュー