Jawad Abbasi
Jawad Abbasi

Reputation: 146

Instagram Login Issue in Flutter: Working on iOS but Error in Android

I'm working on a Flutter app that includes Instagram login using a WebView. The code works perfectly on iOS, but I'm encountering an issue on Android.

After successfully obtaining the authorization code from the first API call (https://api.instagram.com/oauth/authorize), I'm passing the code to the second API (https://api.instagram.com/oauth/access_token). However, the second API call returns an error:

{"error_type": "OAuthException", "code": 400, "error_message": "Invalid authorization code"}

Function I'm using for access_code:

Future<bool> getTokenAndUserID() async {
    var url = Uri.parse('https://api.instagram.com/oauth/access_token');
    final response = await http.post(
      url,
      body: {
        'client_id': InstagramConstant.clientID,
        'redirect_uri': InstagramConstant.redirectUri,
        'client_secret': InstagramConstant.appSecret,
        'code': authorizationCode,
        'grant_type': 'authorization_code'
      },
      headers: {
        // 'accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    );
    log('Response From oauth/access_token API : ${response.body}');
    accessToken = json.decode(response.body)['access_token'];
    // log('$accessToken');
    userID = json.decode(response.body)['user_id'].toString();
    return (accessToken != null && userID != null) ? true : false;
  }

I'm using flutter_webview_plugin: ^0.4.0 for webview.

Upvotes: 2

Views: 75

Answers (0)

Related Questions