Kaan Karamanoğlu
Kaan Karamanoğlu

Reputation: 189

Get Access Token with REST API in flutter/dart

I cannot take access token with this getData() function, it return "Bad Request - Invalid Hostname". How can ı fix this problem ? Am ı change Future<> method, async or http methods ?

Here is my main.dart :

Future<HttpClient> getData() async {

  Map<String, String> connection = {
    'grant_type': 'string',
    'branchcode': 'string',
    'password': 'string',
    'username': 'string',
    'dbname': 'string',
    'dbuser': 'string',
    'dbpassword': 'string',
    'dbtype': 'string+'
  };

  var uri = Uri.http("192.168.1.44:7070","api/v2/token",connection);
  http.Response r = await http.get(uri);
  print(r.statusCode);
  print(r.body);
}

ERROR !

I/flutter ( 9316): 400
I/flutter ( 9316): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
I/flutter ( 9316): <HTML><HEAD><TITLE>Bad Request</TITLE>
I/flutter ( 9316): <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
I/flutter ( 9316): <BODY><h2>Bad Request - Invalid Hostname</h2>
I/flutter ( 9316): <hr><p>HTTP Error 400. The request hostname is invalid.</p>
I/flutter ( 9316): </BODY></HTML>

Upvotes: 2

Views: 15915

Answers (2)

Kaan Karamanoğlu
Kaan Karamanoğlu

Reputation: 189

If you want to get token from rest api you have to post your connection with http.post() method.In this method you can easily declare the connection string in body field.I tried and it is working.

Here is my code block:

   Future<Null> getData() async {

        var url = "http://192.168.1.23:7070/api/v2/token";
        http.post(url, body:{
          "grant_type": "string",
          "branchcode": "string",
          "password": "string",
          "username": "string",
          "dbname": "string",
          "dbuser": "string",
          "dbpassword": "string",
          "dbtype": "string"
        }).then((response){
          print("Response Status: ${response.statusCode}");
          print("Response Body: ${response.body}");
        });
    }

Upvotes: 3

M.Ali
M.Ali

Reputation: 10245

I have tried to get the access token using unsplash api and it worked fine. I have used two plugins. first one, url_launcher, it allows you to open the url in a browser. you should put the registration url and once the user has successfully registered, user is redirected to your app. how? using the second plugin uni_links. once the user return back you start to extract the access token.

Launch the url that allow user to register

_launchURL() async {
    var registerUrl= Constants.registerUrl;
    if (await canLaunch(registerUrl)) {
      await launch(registerUrl);
    } else {
      throw 'Could not launch $loginUrl';
    }
  }

when the user finish the process. your app should listen for this step

@override
  initState() {
    super.initState();
    initUniLinks();
  }




Future<Null> initUniLinks() async {
    getUriLinksStream().listen((Uri uri) {
      if (uri != null) {
        setState(() {
          String code = uri.queryParameters["code"];
          print("code:" + code);
          if (code != null) {
            _getAccessToken(Constants.clientId, Constants.clientSecret,
                Constants.redirectURI, code, "authorization_code");
          }
        });
      }
    }, onError: (err) {
      print("error:" + err.toString());
    });
  }



_getAccessToken(String client_id, String client_secret, String redirect_uri,
      String code, String grant_type) async {
    var url = "https://unsplash.com/oauth/token";

    var query_params = {
      "client_id": client_id,
      "client_secret": client_secret,
      "redirect_uri": redirect_uri,
      "code": code,
      "grant_type": grant_type
    };

    var response = await http.post(url, body: query_params);

    Map<String, dynamic> map = json.decode(response.body);
    var accessToken = AccessToken.fromJson(map);
    _saveAccessTokenToPrefs(accessToken);
  }

Upvotes: 0

Related Questions