サーバーサイドの処理
それでは,
前提条件として,
認可コードの取得
認可コードを得るため,
- https://
oauth. live. com/ authorize?client_ id=CLIENT_ ID &scope=SCOPES&response_type=code&redirect_ uri=REDIRECT_ URL
パラメーターは次の通りです。
名前 | 説明 |
---|---|
client_ | クライアントID |
scope | スコープ 複数の場合はスペース 例: wl. |
response_ | レスポンスの種類 認可コードの場合 code |
redirect_ | リダイレクトURL ドメインは登録情報と一致している必要があります。 |
さらに次のパラメーターもオプションで指定できます。
名前 | 説明 |
---|---|
display | 認可画面の表示形式 popup, touch |
locale | 認可画面の国・ 例: ja |
state | アプリで使用する任意の値 指定した場合, |
display=touchを指定すると,
認可サーバーがリダイレクトしたとき,
- http://
example. jp/ callback. php?code=[AuthorizationCode]
認可コードを取得するまでのPHPコードは,
<?php
define('CLIENT_ID', 'xxxxx');
define('CLIENT_SECRET', 'xxxxx');
define('REDIRECT_URI', 'http%3A%2F%2Fexample.jp%2F');
if (isset($_GET['code'])) {
// (認可コードからアクセストークン取得 処理を記述)
}
if (!$msg) {
$signInUri = 'https://oauth.live.com/authorize' .
'?client_id=' . CLIENT_ID .
'&scope=wl.signin&20wl.offline_access' .
'&response_type=code' .
'&display=popup' .
'&locale=ja' .
'&redirect_uri=' . REDIRECT_URI;
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>Server-Side Sample</title>
</head>
<body>
<?php
if ($signInUri) {
?>
<div><a href="<?php echo $signInUri; ?>">サインイン</a></div>
<?php
} else {
echo $msg;
}
?>
</body>
</html>
define部分はアプリにあわせて変更してください。上記のコードは,
アクセストークンの取得
次は,
- https://
oauth. live. com/ token?client_ id=CLIENT_ ID &redirect_uri=REDIRECT_ URL &client_secret=CLIENT_ SECRET &code=AUTHORIZATION_CODE &grant_type=authorization_ code
パラメーターは次の通りです。
名前 | 説明 |
---|---|
client_ | クライアントID |
redirect_ | リダイレクトURL ドメインは登録情報と一致している必要があります。 |
client_ | クライアントシークレット |
code | 認可コード |
grant_ | 認可コードを使用する場合 authorization_ |
レスポンスは,
{
"access_token": "xxxxx",
"refresh_token": "xxxxx",
"expires_in": 3600,
"scope": "wl.signin wl.offline_access",
"token_type": "bearer"
}
アクセストークン