Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@tkgling

iksm_sessionを取得するための認証リンク

ほしかったもの

Swiftでニンテンドーアカウントにアクセスしてiksm_sessionを取得したかった。

splatnet2statinkでは実装されているのだが、そのコードをPHP向けに書き直した感じです。

ただ、これだけでは足りなくてNexus氏のflapg APIとfrozenpandaman氏のs2s APIが別途必要になります。

実装

PHPでイカのコードを書くだけ。

<?php
function urlsafe_b64encode($val) {
  $val = base64_encode($val);
  return str_replace(["/","+","="], ["_","-",""], $val);
}
$auth_state = urlsafe_b64encode(random_bytes(36));
$auth_code_verifier = urlsafe_b64encode(random_bytes(32));
$auth_cv_hash = hash("sha256", $auth_code_verifier);
$auth_code_challenge = urlsafe_b64encode(hex2bin($auth_cv_hash));

$base_url = "https://accounts.nintendo.com/connect/1.0.0/authorize?";

$param = array( 
    "state" => $auth_state,
    "redirect_uri" => "npf71b963c1b7b6d119://auth",
    "client_id" => "71b963c1b7b6d119",
    "scope" => "openid user user.birthday user.mii user.screenName",
    "response_type" => "session_token_code",
    "session_token_code_challenge" => $auth_code_challenge,
    "session_token_code_challenge_method" => "S256",
    "theme" => "login_form"
);
$auth_url = $base_url . http_build_query($param);
$response = array(
    "auth_code_verifier" => $auth_code_verifier,
    "auth_url" => $auth_url);

header('content-type: application/json; charset=utf-8');
echo(json_encode($response, JSON_UNESCAPED_SLASHES));
?>

リダイレクトURIがURLエンコードされてしまうので微妙にダサい感じになるがこれで動きます。

完成したもの

自分のリリースしたアプリでどうしてもそういう機能が欲しかったのでこういうのを完成させました。

AWS上のサーバなのでご利用は計画的に。

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
tkgling

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?