Last active
April 2, 2023 07:32
-
-
Save Atsumi3/e2a75af8a8b08cc57871bd91e0668fe5 to your computer and use it in GitHub Desktop.
Flutter で Yahoo Japan へのログインをする Dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
pubspec.yaml | |
dependencies: | |
flutter_inappwebview: 5.5.0 | |
http: ^0.13.5 | |
*/ | |
// https://developer.yahoo.co.jp/yconnect/v2/authorization_code/ | |
import 'dart:convert'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; | |
import 'package:http/http.dart' as http; | |
class YahooAccessToken { | |
final String accessToken; | |
final String tokenType; | |
final String idToken; | |
final int expiresIn; | |
final String refreshToken; | |
YahooAccessToken({ | |
required this.accessToken, | |
required this.tokenType, | |
required this.idToken, | |
required this.expiresIn, | |
required this.refreshToken, | |
}); | |
factory YahooAccessToken.fromJsonString(String jsonString) { | |
final json = jsonDecode(jsonString); | |
return YahooAccessToken( | |
accessToken: json["access_token"], | |
tokenType: json["token_type"], | |
idToken: json["id_token"], | |
expiresIn: json["expires_in"], | |
refreshToken: json["refresh_token"], | |
); | |
} | |
} | |
mixin YahooLoginDialogMixin { | |
void showYahooLoginDialog({ | |
required BuildContext context, | |
required Function(YahooAccessToken?) onLogin, | |
}) { | |
showDialog( | |
context: context, | |
builder: (_) { | |
return AlertDialog( | |
content: _YahooLoginDialogContent( | |
onLogin: onLogin, | |
), | |
); | |
}, | |
); | |
} | |
} | |
class _YahooLoginDialogContent extends StatefulWidget { | |
final Function(YahooAccessToken? token) onLogin; | |
const _YahooLoginDialogContent({ | |
required this.onLogin, | |
}); | |
@override | |
State<StatefulWidget> createState() => _YahooLoginDialogContentState(); | |
} | |
class _YahooLoginDialogContentState extends State<_YahooLoginDialogContent> { | |
final String _clientId = ""; | |
final String _secret = ""; | |
final String _redirectUri = ""; | |
final String _authUri = | |
"https://auth.login.yahoo.co.jp/yconnect/v2/authorization"; | |
final String _tokenUri = "https://auth.login.yahoo.co.jp/yconnect/v2/token"; | |
String get _authRequestUri { | |
final params = { | |
"client_id": _clientId, | |
"redirect_uri": Uri.encodeFull(_redirectUri), | |
"response_type": "code", | |
"scope": "openid email profile", | |
"bail": 1, | |
}.entries.map((e) => "${e.key}=${e.value}").join("&"); | |
return "$_authUri?$params"; | |
} | |
Future<void> _getAccessToken(String code) async { | |
final response = await http.post( | |
Uri.parse(_tokenUri), | |
headers: { | |
"Authorization": | |
"Basic ${base64Encode(utf8.encode("$_clientId:$_secret"))}", | |
}, | |
body: { | |
"grant_type": "authorization_code", | |
"code": code, | |
"redirect_uri": _redirectUri, | |
}, | |
); | |
if (response.statusCode == 200) { | |
final token = YahooAccessToken.fromJsonString(response.body); | |
widget.onLogin(token); | |
} else { | |
widget.onLogin(null); | |
} | |
} | |
/// yahoo login | |
@override | |
Widget build(BuildContext context) { | |
return SizedBox( | |
width: double.maxFinite, | |
child: InAppWebView( | |
initialUrlRequest: URLRequest(url: Uri.parse(_authRequestUri)), | |
onLoadStop: (controller, url) async { | |
if (url.toString().startsWith(_redirectUri)) { | |
final code = Uri.parse(url.toString()).queryParameters["code"]; | |
if (code != null) { | |
await _getAccessToken(code); | |
} else { | |
widget.onLogin(null); | |
} | |
if (!mounted) return; | |
Navigator.of(context).pop(); | |
} | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment