LoginSignup
1
1

More than 5 years have passed since last update.

Yahoo!ID連携がうまく行かない。サンプル通りだがエラーになる。

Posted at

お世話になっております。
Yahoo!ID連携の構築について質問です。

■SDKを利用したPHPによる導入方法
http://developer.yahoo.co.jp/yconnect/server_app/php/

記述通り行っているのですが、ログインボタン押下後、callback.phpにリダイレクトされ
Authorization Codeを取得を取得とありますが、エラーで取得ができません。
下記にコードを記載しますので、お分かりの方がいらっしゃいましたら修正点をお願いします。

Notice: Undefined variable: state in /var/www/html/fmit-kk.com/admin/callback.php on line 18
Fatal error: Uncaught YConnect\Exception\TokenException: not_matched_state ( the state did not match ) thrown in /var/www/html/fmit-kk.com/admin/vendor/yahoojapan/yconnect-php-sdk/lib/YConnect/YConnectClient.php on line 188
admin_header.php
<span class="yconnectLogin"></span>
<script type="text/javascript">
    window.yconnectInit = function() {
        YAHOO.JP.yconnect.Authorization.init({
            button: {
                format: "image",
                type: "a",
                width: 64,
                height: 36,
                className: "yconnectLogin"
            },
            authorization: {
                clientId: "app_id",
                redirectUri: "http://fmit-kk.com/admin/callback.php",
                scope: "openid",
                state: "44Oq44Ki5YWF44Gr5L+644Gv44Gq44KL77yB",
                nonce: "5YOV44Go5aWR57SE44GX44GmSUTljqjjgavjgarjgaPjgabjgog="
            },
            onError: function(res) {
                // エラー発生時のコールバック関数
            },
            onCancel: function(res) {
                // 同意キャンセルされた時のコールバック関数
            }
        });
    };
    (function(){
    var fs = document.getElementsByTagName("script")[0], s = document.createElement("script");
    s.setAttribute("src", "https://s.yimg.jp/images/login/yconnect/auth/1.0.0/auth-min.js");
    fs.parentNode.insertBefore(s, fs);
    })();
</script>
callback.php
<?php
ini_set( 'display_errors', 1 );
require './vendor/autoload.php';

use YConnect\Credential\ClientCredential;
use YConnect\YConnectClient;

// アプリケーションID, シークレッvト
$client_id     = "app_id";
$client_secret = "シークレット";
$redirect_uri  = "http://fmit-kk.com/admin/ys_index.php";

$cred = new ClientCredential( $client_id, $client_secret );
$client = new YConnectClient( $cred );

try {
    // Authorization Codeを取得
    $code_result = $client->getAuthorizationCode( $state );

    // Tokenエンドポイントにリクエスト
    $client->requestAccessToken( $redirect_uri, $code_result );

    // アクセストークン, リフレッシュトークンを取得
    $access_token  = $client->getAccessToken();
    $refresh_token = $client->getRefreshToken();

} catch ( TokenException $e ) {
    // 再度ログインして認可コードを発行してください
}
echo $access_tokene;
echo $refresh_token;
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1