PHPバージョン5.6
フレームワーク等は使用しておりません。
LINE Login v2.1にてソーシャルログインを実装したいと考えております。
下記のlogin.phpからLINEにログインして、LINE側から認可コードを取得
するところまではできるのですが、callback.phpにてアクセストークンを取得
しようとしたところ、下記のエラーが出てしまいます。
エラー部分
Warning: file_get_contents(https://api.line.me/oauth2/v2.1/token): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in
同様にcurlを使用した別バージョンも試してみたのですが、こちらでもエラーが出てしまいます。
そのエラーは
Notice: Undefined property: stdClass::$access_token in
とにかく原因がわからずに困っております。
お力をお貸し頂けないでしょうか。
宜しくお願い致します。
参考サイト
・https://github.com/line/line-login-guide-faq
php
1//ログイン部分 login.php 2define('LINE_CLIENT_ID', '*******'); 3define('Redirect_URL', 'https://samle.com/callback.php'); 4define('State', '123abc'); 5$url = 'https://access.line.me/oauth2/v2.1/authorize?scope=openid%20profile&response_type=code&client_id=' . getenv('LINE_CLIENT_ID') . '&redirect_uri=' . getenv(Redirect_URL). '&state=' . getenv(State); 6echo '<a href=' . $url . '><button class="btn">Login using OpenID Connect</button></a>' . PHP_EOL;
php
1//callback.php 2 3define("LINE_CLIENT_ID", "************"); 4define("LINE_CLIENT_SECRET", "*************"); 5define('Redirect_URL', 'https://samle.com/callback.php'); 6 7 $url = 'https://api.line.me/oauth2/v2.1/token'; 8 $data = array( 9 'grant_type' => 'authorization_code', 10 'client_id' => getenv('LINE_CLIENT_ID'), 11 'client_secret' => getenv('LINE_CLIENT_SECRET'), 12 'code' => $_GET['code'], 13 'redirect_uri' => Redirect_URL 14 ); 15$data = http_build_query($data, '', '&'); 16 $header = array( 17 'Content-Type: application/x-www-form-urlencoded', 18 ); 19 $context = array( 20 'http' => array( 21 'method' => 'POST', 22 'header' => implode('\r\n', $header), 23 'content' => $data, 24 ) 25 ); 26 $resultString = file_get_contents($url, false, stream_context_create($context)); 27 $result = json_decode($resultString, true); 28 29
php
1//callback.php 2curlを使用したもの 3$postData = array( 4 'grant_type' => 'authorization_code', 5 'code' => $_GET['code'], 6 'redirect_uri' => 'https://samle.com/callback.php', 7 'client_id' => '********', 8 'client_secret' => '********' 9); 10 11$ch = curl_init(); 12 13curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 14curl_setopt($ch, CURLOPT_URL, 'https://api.line.me/oauth2/v2.1/token'); 15curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 16curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); 17curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 18 19$response = curl_exec($ch); 20curl_close($ch); 21 22$json = json_decode($response); 23$accessToken = $json->access_token; 24
回答1件
あなたの回答
tips
プレビュー