Skip to content

Instantly share code, notes, and snippets.

@WillyShakes
Created February 21, 2018 06:18
Show Gist options
  • Save WillyShakes/df589e92f3a5d4e5304b32e6b83977f7 to your computer and use it in GitHub Desktop.
Save WillyShakes/df589e92f3a5d4e5304b32e6b83977f7 to your computer and use it in GitHub Desktop.
instagran authentication
Future<Token> getToken(String appId, String appSecret) async {
Stream<String> onCode = await _server();
String url =
"https://api.instagram.com/oauth/authorize?client_id=$appId&redirect_uri=http://localhost:8585&response_type=code";
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.launch(url);
final String code = await onCode.first;
final http.Response response = await http.post(
"https://api.instagram.com/oauth/access_token",
body: {"client_id": appId, "redirect_uri": "http://localhost:8585", "client_secret": appSecret,
"code": code, "grant_type": "authorization_code"});
flutterWebviewPlugin.close();
return new Token.fromMap(JSON.decode(response.body));
}
Future<Stream<String>> _server() async {
final StreamController<String> onCode = new StreamController();
HttpServer server =
await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8585);
server.listen((HttpRequest request) async {
final String code = request.uri.queryParameters["code"];
request.response
..statusCode = 200
..headers.set("Content-Type", ContentType.HTML.mimeType)
..write("<html><h1>You can now close this window</h1></html>");
await request.response.close();
await server.close(force: true);
onCode.add(code);
await onCode.close();
});
return onCode.stream;
}
class Token {
String access;
String id;
String username;
String full_name;
String profile_picture;
Token.fromMap(Map json){
access = json['access_token'];
id = json['user']['id'];
username = json['user']['username'];
full_name = json['user']['full_name'];
profile_picture = json['user']['profile_picture'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment