1
votes

edit: I found the solution and answered the question.

I'm trying POST to Instagram's API in my app. The POST works fine in Postman, but in my flutter code I always get a error response: {error_type: OAuthException, code: 400, error_message: Invalid platform app} . I must be doing something wrong in my flutter POST but I can't figure out what.

Here is my Postman POST (that works every time) along with the correct body response: Postman Data Postman Data 2

And here is my Dart/Flutter code that gives me the error:

        var clientID = '708XXXXXXXX';
        var clientSecret = '37520XXXXXXXXXXXXX';
        var grantType = 'authorization_code';
        var rediUrl = 'https://httpstat.us/200';
        
        var urlTwo = 'https://api.instagram.com/oauth/access_token';
        var body = json.encode({
          'client_id': clientID,
          'client_secret': clientSecret,
          'grant_type': grantType,
          'redirect_uri': rediUrl,
          'code': _accessCode
        });
                
        print('Body: $body');

        var response = await http.post(urlTwo,
          headers: {
            'Accept': '*/*',
            'Content-Type': 'application/json',
          },
          body: body,
        );

        print(json.decode(response.body));

I read about the error on a different thread and they say to post as x-www-formurlencoded.. When I switch the Content-Type to this, I get the same error.

The error I get in Flutter:

{error_type: OAuthException, code: 400, error_message: Invalid platform app}

I triple checked and I am using all the same value in Postman and Flutter. I've tried the values as Strings and numbers.

When I print body:

Body: {"client_id":"70000edited","client_secret":"37520634edited","grant_type":"authorization_code","redirect_uri":"https://httpstat.us/200","code":"AQCs-H3cIU0aF1o2KkltLuTmVMmJ-WJnZIhb9ryMqYedited"}
2
Did you try to change the redirect_uri like this: localhost:5001 - Akif
@Akif no, the redirect-uri has to match the URL I set in my Facebook Developer dashboard, the one I have set works fine in Postman. The one I use automatically send a 200 response for testing. - August Kimo

2 Answers

0
votes

try this once

final response = await http.post(urlTwo,
   body: body,
   headers: {
     "Accept": "application/json",
     "Content-Type": "application/x-www-form-urlencoded"
   },
   encoding: Encoding.getByName("utf-8"));
0
votes

I've finally got it working using a Map. Which is weird because other attempts I also used a map (I've been trying numerous stack overflow answers). Anyways, here is the working code I used:

        var urlTwo = 'https://api.instagram.com/oauth/access_token';
        var clientID = '7080000';
        var clientSecret = '375XXXXXX';
        var grantType = 'authorization_code';
        var rediUrl = 'https://httpstat.us/200';

        var map = new Map<String, dynamic>();
        map['client_id'] = clientID;
        map['client_secret'] = clientSecret;
        map['grant_type'] = grantType;
        map['redirect_uri'] = rediUrl;
        map['code'] = _accessCode;

        http.Response response = await http.post(
          urlTwo,
          body: map,
        );

        var respData = json.decode(response.body);
        print(respData);