$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => '*************************', // The client ID assigned to you by the provider
'clientSecret' => '*************************', // The client password assigned to you by the provider
'redirectUri' => 'http://********/your-redirect-url/',
'urlAuthorize' => 'https://app.misoca.jp/oauth2/authorize',
'urlAccessToken' => 'https://app.misoca.jp/oauth2/token',
'urlResourceOwnerDetails' => '',
'scopes'=>'write'
]);
// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {
// Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters
// (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl();
// Get the state generated for you and store it to the session.
$_SESSION['oauth2state'] = $provider->getState();
// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {
if (isset($_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
}
exit('Invalid state');
} else {
try {
// Try to get an access token using the authorization code grant.
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// APIにリクエストを送信(見積もりの作成)
// $mitsumoriに見積もりの配列を入れます。詳細はMisocaのドキュメントを確認
$request = $provider->getAuthenticatedRequest(
'POST',
'https://app.misoca.jp/api/v3/estimate',
$accessToken,
array(
"body"=>json_encode($mitsumori),
'headers' => array(
'Content-Type' => 'application/json;charset=UTF-8', // このヘッダーがないと返りがうまくいかない
),
)
);
// APIからのレスポンスを取得
$response = $provider->getResponse($request);
// レスポンスのボディ部分を取得
$data = $response->getBody()->getContents();
// JSONで返ってきているのでデコード
$result = json_decode($data, true);
session_destroy();// セッションを削除
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token or user details.
exit($e->getMessage());
}
}